TechnologiesSwiftUIOrnaments and Glass Effects

AutomaticGlassBackgroundEffect struct

iOSmacOStvOSwatchOSvisionOS✓ renders

The automatic glass background effect.

How it works

AutomaticGlassBackgroundEffect is the glass background style SwiftUI applies when you ask the system to decide for you, letting the platform choose whether a translucent glass material is drawn behind a view and how prominently it appears. Rather than committing a view to an always-on or always-off glass treatment, the automatic effect defers to the surrounding context — the platform, the ornament or container the view lives in, and the current rendering environment — so the same view reads correctly wherever it's placed. Reach for it when you want a view to participate in the system's glass language without hard-coding a fixed appearance, which keeps the result consistent as Apple tunes the material across releases.

  1. Apply the effect with glassBackgroundEffect(displayMode:)

    The effect is attached as a view modifier rather than constructed directly. Calling .glassBackgroundEffect(displayMode:) on a view places a glass material behind its content; here it wraps the VStack of the Image and two Text views so the whole card sits on glass.

  2. Select automatic behavior via .automatic

    Passing displayMode: .automatic is what selects AutomaticGlassBackgroundEffect. The .automatic case hands the decision back to SwiftUI, which resolves an appropriate glass appearance for the current context instead of forcing it visible or hidden the way an explicit mode would.

  3. Pad the content before the glass is drawn

    Because the modifier reads the view it's applied to, the glass shape follows that view's bounds. The .padding(24) placed before .glassBackgroundEffect(displayMode:) insets the content so the glass material extends around it, while the trailing .padding() adds breathing room outside the glass card.

  4. Let the effect adapt to its container

    The defining trait of the automatic effect is that it adjusts to where the view is hosted — the surrounding Text("Adapts to its context") names exactly this behavior. The same glassBackgroundEffect(displayMode: .automatic) call can render differently depending on the platform and the ornament or surface it appears within.

Try it — Change .glassBackgroundEffect(displayMode: .automatic) to .glassBackgroundEffect(displayMode: .always) and compare the card's background — the automatic mode may suppress or soften the glass in contexts where .always keeps it fully drawn.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

AutomaticGlassBackgroundEffect.swift
struct AutomaticGlassBackgroundEffectDemo: View {
    var body: some View {
        VStack(spacing: 8) {
            Image(systemName: "sparkles")
                .font(.largeTitle)
            Text("Glass Card")
                .font(.headline)
            Text("Adapts to its context")
                .font(.caption)
                .foregroundStyle(.secondary)
        }
        .padding(24)
        .glassBackgroundEffect(displayMode: .automatic)
        .padding()
    }
}
Live preview
Glass Card Adapts to its context
Glass Card Adapts to its context
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →