Skip to content

Instantly share code, notes, and snippets.

@Chronos2500
Forked from Rukh/BackdropBlurView.swift
Last active July 22, 2025 20:07
Show Gist options
  • Select an option

  • Save Chronos2500/bf66e2a6c441ae3921e688594ed556d7 to your computer and use it in GitHub Desktop.

Select an option

Save Chronos2500/bf66e2a6c441ae3921e688594ed556d7 to your computer and use it in GitHub Desktop.
UIVisualEffectView with any blur radius in SwiftUI
//
// BackdropBlurView.swift
//
// Created by Dmitry Gulyagin on 20.09.2022.
//
import SwiftUI
/// A View which content reflects all behind it
struct BackdropView: UIViewRepresentable {
public func makeUIView(context: Context) -> UIVisualEffectView {
let view = UIVisualEffectView()
let blur = UIBlurEffect()
let animator = UIViewPropertyAnimator()
animator.addAnimations { view.effect = blur }
animator.fractionComplete = 0
animator.stopAnimation(false)
animator.finishAnimation(at: .current)
return view
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) { }
}
/// A transparent View that blurs its background
struct BackdropBlurView: View {
let radius: CGFloat
@ViewBuilder
var body: some View {
BackdropView().blur(radius: radius, opaque: true)
}
}
struct ContentView: View {
var body: some View {
Text("Hello, World!")
.padding()
.backdropEffect { backdrop in
backdrop
.blur(radius: 30, opaque: true)
.saturation(10.8)
}
}
}
extension View {
func backdropEffect(@ViewBuilder _ backdropBuilder: (Backdrop) -> some View) -> some View {
background {
backdropBuilder(
Backdrop()
)
}
}
}
struct Backdrop: UIViewRepresentable {
func makeUIView(context: Context) -> some UIView {
return BackdropLayerView()
}
func updateUIView(_ uiView: UIViewType, context: Context) {
}
class BackdropLayerView: UIView {
override class var layerClass: AnyClass {
return NSClassFromString("CABackdropLayer") as! CALayer.Type
}
}
}
@Chronos2500
Copy link
Author

@Chronos2500
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment