How to blur a view
About 2 min
How to blur a view êŽë š
SwiftUI by Example
Back to Home
How to blur a view | SwiftUI by Example
How to blur a view
Updated for Xcode 15
The blur()
modifier lets us apply a real-time Gaussian blur to our views, at a strength of our choosing.
For example, this creates a 300x300 profile picture, then adds a 20-point Gaussian blur:
Image("dog")
.resizable()
.frame(width: 300, height: 300)
.blur(radius: 20)
You can blur anything you want, including text views:
Text("Welcome to my SwiftUI app")
.blur(radius: 2)
Blurring is extremely efficient, and you can adjust it dynamically just like any other kind of state. For example, this lets you try adjusting the blur of our text dynamically by dragging around a slider:
struct ContentView: View {
@State private var blurAmount = 0.0
var body: some View {
VStack {
Text("Drag the slider to blur me")
.blur(radius: blurAmount)
Slider(value: $blurAmount, in: 0...20)
}
}
}
Similar solutionsâŠ
SwiftUI tips and tricks | SwiftUI by Example
SwiftUI tips and tricks
How to convert a SwiftUI view to an image | SwiftUI by Example
How to convert a SwiftUI view to an image
How to make a view dismiss itself | SwiftUI by Example
How to make a view dismiss itself
How to dynamically adjust the appearance of a view based on its size and location | SwiftUI by Example
How to dynamically adjust the appearance of a view based on its size and location
How to create and compose custom views | SwiftUI by Example
How to create and compose custom views