skip to content

In SwiftUI, how do you put a frosted Material card over a workout photo, and when do you use clipShape versus mask?

level: middleimportance: should knowfreq 50%

answer

  1. a ShapeStyle that blurs
  2. background with a shape
  3. geometry versus alpha
  4. fade needs partial transparency

basics

~20 s

Back the card with a Material, as in background(.ultraThinMaterial, in: .rect(cornerRadius: 16)), so it blurs the photo behind it. clipShape cuts a view to a shape's outline; mask applies another view's alpha, so it can fade.

solid answer

~40 s

A `Material` is a `ShapeStyle` (iOS 15) that blurs whatever is drawn behind the view, in five weights from `.ultraThinMaterial` to `.ultraThickMaterial`. I overlay the run's name and distance on the photo and give that text `.background(.ultraThinMaterial, in: .rect(cornerRadius: 16))`, which fills a rounded shape behind it in one step. For the photo, `clipShape(.rect(cornerRadius: 24))` keeps only what lies inside the shape: each pixel is in or out. `mask { … }` instead uses another view's alpha channel, so a `LinearGradient` from opaque to clear fades the photo toward its bottom edge, which `clipShape` cannot do. For plain rounding I use `clipShape`; `cornerRadius(_:)` is marked for deprecation in favour of it.

code

swift · 37 lines
swift
import SwiftUI

struct RunSummaryCard: View {
    var body: some View {
        Image("trail-run")
            .resizable()
            .scaledToFill()
            .frame(height: 260)
            .clipShape(.rect(cornerRadius: 24))
            .overlay(alignment: .bottomLeading) {
                VStack(alignment: .leading, spacing: 4) {
                    Text("Morning run")
                        .font(.headline)
                    Text("8.2 km in 41 min")
                        .font(.subheadline)
                        .foregroundStyle(.secondary)
                }
                .padding()
                .frame(maxWidth: .infinity, alignment: .leading)
                .background(.ultraThinMaterial, in: .rect(cornerRadius: 16))
                .padding(12)
            }
    }
}

struct FadedHeader: View {
    var body: some View {
        Image("trail-run")
            .resizable()
            .scaledToFill()
            .frame(height: 200)
            .clipped()
            .mask {
                LinearGradient(colors: [.black, .clear], startPoint: .center, endPoint: .bottom)
            }
    }
}

go deeper

for a junior

Recall the five material weights and that background(_:in:) fills a shape behind a view with any style, including a material.

for a middle

Explain the difference between clipping to a shape's geometry and masking with a view's alpha, and when only a mask can produce the effect.

for a senior

Show you check legibility of text on materials over real photos, honour Reduce Transparency, and keep modifier order right so the photo, not the card, is clipped.

for a principal

Decide where translucent surfaces belong in the product's visual language, and set the rule for when a card gets a material versus an opaque fill.

## Materials are shape styles A **material** in SwiftUI is a `Material` value, a `ShapeStyle` available since iOS 15. Where a `Color` paints a fixed colour, a material renders a translucent layer that **blurs whatever is drawn behind the view**. SwiftUI offers five weights, from most to least see-through: - `.ultraThinMaterial` - `.thinMaterial` - `.regularMaterial` - `.thickMaterial` - `.ultraThickMaterial` There is also `.bar`, the material used behind bars, which is unavailable on tvOS and watchOS. Because a material is a `ShapeStyle`, it goes anywhere a style goes: `background(_:)`, `background(_:in:)`, `fill(_:)`. `Rectangle().fill(.thinMaterial)` is a material-filled shape you can place in a `ZStack` like any other view. Without a shape, `background(_:ignoresSafeAreaEdges:)` fills the view's whole frame, and its edges parameter defaults to `.all`, so the style extends into safe areas the view touches. That suits a full-width bar; a floating card wants `background(_:in:)` with a shape instead. A material only looks frosted when there is something interesting behind it. Over a flat colour, a blur of that colour is the same colour, so the card reads as a plain tinted panel. Over a photo, a map or scrolling content it gives the familiar frosted look. ## Placing the card For a workout summary — a photo with the run's name and distance on a frosted card — the usual structure is: 1. Size and crop the photo, then round its corners with `clipShape(.rect(cornerRadius: 24))`. 2. Put the text in an `overlay(alignment: .bottomLeading)`. 3. Pad the text and give it `.background(.ultraThinMaterial, in: .rect(cornerRadius: 16))`. 4. Use hierarchical styles such as `.secondary` for the second line, so it stays legible against the blur. `background(_:in:fillStyle:)` draws the style inside a shape behind the view, so it replaces the older two-step habit of a background followed by a rounding modifier. `cornerRadius(_:)` is marked for deprecation in the SDK with the message "Use `clipShape` or `fill` instead." ## clipShape: keep what is inside a shape `clipShape(_:style:)` keeps the parts of a view that fall **inside a shape's geometry** and discards the rest; the clipping shape itself is invisible. Each pixel is in or out, with antialiasing at the edge. Apple's documentation notes that the shape fills the view's frame, so `clipShape(.rect(cornerRadius: 24))` on a 260-point-tall photo rounds that frame. `clipped()` is the rectangular special case: it clips to the view's bounding frame, which is what an image scaled to fill needs so it does not overflow. ## mask: use another view's alpha `mask(alignment:_:)` (iOS 15) takes a view builder and applies the **alpha channel** of the view it returns. Its colours are ignored; only opacity matters. That makes partial transparency possible: - a `LinearGradient` from `.black` to `.clear` fades a photo out toward its bottom edge; - a `Text` used as a mask lets a photo show through the letters; - a `Rectangle` at 50 % opacity makes the view half transparent wherever it covers. A mask made of one fully opaque shape gives the same result as `clipShape` with that shape, but `clipShape` states the intent and takes a shape directly. | Modifier | Decides visibility by | Partial transparency | Typical use | |---|---|---|---| | `clipShape(_:style:)` | a shape's outline | no, only antialiased edges | rounded cards, circular avatars | | `clipped()` | the view's frame | no | images scaled to fill | | `mask(alignment:_:)` | another view's alpha | yes | fades, text cut-outs | ## Pitfalls - A material **on top of** content does nothing to that content; it blurs what is behind the view it backs. - `clipShape(LinearGradient(...))` does not compile, because a gradient is a `ShapeStyle`, not a `Shape`; fading needs `mask`. - Text on a thin material over a busy photo can lose contrast; pick a thicker material or add a scrim, and check the card in both light and dark appearance. - Clipping the card instead of the photo leaves the photo's square corners showing around a rounded card; clip the view whose edges you want rounded. - People who turn on Reduce Transparency expect opaque surfaces. SwiftUI exposes the setting as `accessibilityReduceTransparency` in the environment, and Apple's documentation for it says backgrounds should then be opaque rather than semi-transparent. ```swift import SwiftUI struct LegibleCaption: View { @Environment(\.accessibilityReduceTransparency) private var reduceTransparency var body: some View { Text("Morning run") .padding() .background( reduceTransparency ? AnyShapeStyle(.background) : AnyShapeStyle(.ultraThinMaterial), in: .rect(cornerRadius: 16) ) } } ```

  • Why does a material card look like flat grey on a plain background?
    A material blurs what is drawn behind the view. Over a solid colour, the blur of that colour is the same colour, so the card reads as a tinted panel with no frosted texture. It earns its look over photos, maps or scrolling content; on a plain background a semantic style such as `.background` is the honest choice.
  • What should a material card do when Reduce Transparency is on?
    Read `@Environment(\.accessibilityReduceTransparency)`. Apple's documentation for that value says backgrounds should then be opaque rather than semi-transparent, so a card whose legibility depends on a blurred photo should switch to an opaque style, for example by choosing between `.background` and `.ultraThinMaterial` with `AnyShapeStyle`.
  • What does MeshGradient add over a LinearGradient behind a card?
    `MeshGradient(width:height:points:colors:)`, new in iOS 18, places a grid of colours at 2D points, `width × height` of them, and interpolates across curved patches, smoothly by default (`smoothsColors: true`). It produces organic multi-colour backdrops a one-axis `LinearGradient` cannot. An app deploying to iOS 17 wraps it in `if #available(iOS 18.0, *)` with a simpler gradient as the fallback.

clipShape works like a cookie cutter: every bit of dough is either inside the outline or discarded. mask works like a dimmer behind each pixel: the mask view's opacity at that point sets how much of the photo shows, from all of it to none.

  • A material blurs the content placed on top of it
  • clipShape can fade a photo if you pass it a gradient
  • mask uses the mask view's colours rather than its alpha
  • cornerRadius(_:) is the recommended way to round a card
  • A material looks frosted over any background, even a flat colour