iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👻

How to Create a Gradient Blur Effect in SwiftUI

に公開

You might not know exactly what I mean, but I want to recreate the UI used in the iOS 18 Photos app or the header section of Globetrotter.

Globetrotter

In SwiftUI, you use Material to achieve a blur effect similar to UIVisualEffect.

https://developer.apple.com/documentation/swiftui/material

Color.clear
  .background(.regularMaterial)

Using it like this applies the blur effect over the entire background.

blurred photo

However, the intensity is uniform with this method, so I want to gradually change the strength of the blur.
I was unsure how to implement this, but while researching something else, I discovered that blend modes can be used in SwiftUI and realized they could be applied here.

Blend Modes

Blend modes are a technique for applying dynamic image processing by calculating the color of overlapping layers based on specific formulas.
They've been around in tools like Photoshop for a long time, so many people might be familiar with them.
It seems they can also be used in CSS these days.
In UI development, destinationOut is frequently used for tasks like masking or "punching out" areas, and it works perfectly for this use case as well.

ZStack {
    Color.clear
      .background(.regularMaterial)

    LinearGradient(
      gradient: Gradient(colors: [.black.opacity(0), .black]),
      startPoint: .top,
      endPoint: .bottom
    )
    .blendMode(.destinationOut)
}
.compositingGroup()

Wrapping it with .compositingGroup() makes the effect visible.

Before & After

Here is how it looks before and after applying the blend mode.

before

after

You can see that the blur is strong at the top and gradually disappears towards the bottom.

The Final Code

public struct BlurBackgroundView: View {
  public init() {}

  let maxHeight: CGFloat = 160

  public var body: some View {
    ZStack {
      Color.clear
        .background(.regularMaterial)
        .frame(maxWidth: .infinity, maxHeight: maxHeight)

      LinearGradient(
        gradient: Gradient(colors: [.black.opacity(0), .black]),
        startPoint: .top,
        endPoint: .bottom
      )
      .blendMode(.destinationOut)
      .frame(maxWidth: .infinity, maxHeight: maxHeight)
    }
    .compositingGroup()
    .frame(maxHeight: maxHeight)
    .ignoresSafeArea()
    .allowsHitTesting(false)
  }
}

You can also use this for the tab bar side by reversing the gradient direction.
I've used allowsHitTesting(false) so that it doesn't affect user interactions.

extension View {
  @ViewBuilder
  public func blurNavigationBar() -> some View {
    ZStack(alignment: .top) {
      self
      BlurBackgroundView()
    }
  }

Creating an extension like this is convenient as it allows you to apply the blur effect only where needed.
Don't forget to clear the navigation bar's background color using Appearance.

Discussion