skip to content

On iOS 18 and later, how do you make a SwiftUI NavigationStack push zoom out of the tapped grid thumbnail?

level: middleimportance: should knowfreq 40%

answer

  1. a navigation transition, not geometry
  2. source and destination share an id
  3. matchedTransitionSource on the thumbnail
  4. zoom(sourceID:in:) on the destination root

basics

~10 s

Mark the thumbnail with matchedTransitionSource(id:in:) and give the destination's root view navigationTransition(.zoom(sourceID:in:)) with the same id and the same @Namespace. Both APIs arrived in iOS 18, and zoom is unavailable on macOS.

solid answer

~30 s

iOS 18 added `navigationTransition(_:)` for customizing a push or sheet presentation. For a zoom, declare one `@Namespace`, attach `matchedTransitionSource(id: photo.id, in: zoom)` to the thumbnail inside the `NavigationLink` label, and attach `navigationTransition(.zoom(sourceID: photo.id, in: zoom))` to the destination's root, outside containers such as `VStack`, as Apple's documentation says. The ids and namespace must match. The `configuration` overload of the source styles it during the zoom, accepting only a `RoundedRectangle` clip, a `Color` background and a shadow. Toolbar items became valid sources in iOS 26. For an iOS 17 deployment target, wrap both calls in `if #available(iOS 18, *)`.

code

swift · 34 lines
swift
struct Photo: Identifiable, Hashable {
    let id: Int
    let assetName: String
}

struct ZoomingGallery: View {
    let photos: [Photo]
    @Namespace private var zoom

    var body: some View {
        NavigationStack {
            ScrollView {
                LazyVGrid(columns: [GridItem(.adaptive(minimum: 100))]) {
                    ForEach(photos) { photo in
                        NavigationLink(value: photo) {
                            Image(photo.assetName)
                                .resizable()
                                .scaledToFit()
                                .matchedTransitionSource(id: photo.id, in: zoom) { source in
                                    source.clipShape(RoundedRectangle(cornerRadius: 8))
                                }
                        }
                    }
                }
            }
            .navigationDestination(for: Photo.self) { photo in
                Image(photo.assetName)
                    .resizable()
                    .scaledToFit()
                    .navigationTransition(.zoom(sourceID: photo.id, in: zoom))
            }
        }
    }
}

go deeper

for a junior

Recall the two modifiers, matchedTransitionSource on the tapped view and navigationTransition(.zoom) on the destination, sharing an id and a @Namespace, and that they need iOS 18.

for a middle

Explain where each modifier goes, including the documented rule to keep navigationTransition outside containers such as VStack, and how to style the source through its configuration closure.

for a senior

Ship it on an iOS 17 deployment target with availability guards, and choose between the zoom and an in-place matchedGeometryEffect based on whether the detail is a real destination.

for a principal

Weigh adopting system zoom transitions across an app: consistent with the platform and cheap to maintain, but gated on iOS 18, so the minimum deployment target decides how much fallback code a team carries.

## What the zoom navigation transition is On iOS 18, SwiftUI added a way to customize how a view is pushed onto a navigation stack or presented, through the **`navigationTransition(_:)`** modifier and the **`NavigationTransition`** protocol. Its most visible style is **zoom**: the destination appears to grow out of the view the user tapped, a grid thumbnail for example, and shrinks back into it on the way out. Before iOS 18, SwiftUI offered no modifier to change a push's standard transition. The zoom is built from two modifiers that share an identifier and a namespace: 1. **`matchedTransitionSource(id:in:)`** marks the view the zoom starts from. Apple describes it as identifying this view as the source of a navigation transition, such as a zoom transition. 2. **`navigationTransition(.zoom(sourceID:in:))`** goes on the destination and names the same id and namespace. Both are declared `@available(iOS 18.0, ...)`, and the zoom style itself is marked unavailable on macOS. ## Where each modifier goes - **The source**: the thumbnail inside the `NavigationLink` label, with an id that identifies the data, usually `photo.id`. - **The destination**: the root of the view the link shows, for example inside `navigationDestination(for:destination:)`. Apple's documentation says to add the modifier to a view that appears within a NavigationStack or a sheet, **outside of any containers such as VStack**. - **The namespace**: one `@Namespace` declared in the view that contains both the grid and the stack, so the source and the destination see the same `Namespace.ID`. ```swift struct Photo: Identifiable, Hashable { let id: Int let assetName: String } struct ZoomingGallery: View { let photos: [Photo] @Namespace private var zoom var body: some View { NavigationStack { ScrollView { LazyVGrid(columns: [GridItem(.adaptive(minimum: 100))]) { ForEach(photos) { photo in NavigationLink(value: photo) { Image(photo.assetName) .resizable() .scaledToFit() .matchedTransitionSource(id: photo.id, in: zoom) { source in source.clipShape(RoundedRectangle(cornerRadius: 8)) } } } } } .navigationDestination(for: Photo.self) { photo in Image(photo.assetName) .resizable() .scaledToFit() .navigationTransition(.zoom(sourceID: photo.id, in: zoom)) } } } } ``` This sample assumes an iOS 18 deployment target. The id and the namespace must match on both sides, or the destination has no matching source to zoom from. ## Styling the source The `matchedTransitionSource(id:in:configuration:)` overload takes a closure that styles the source, and the documentation says modifiers applied there are smoothly interpolated when a zoom starts from it. The 27.0 SDK declares exactly three methods on the configuration, and two of them are deliberately narrow: | Method | What the SDK accepts | |---|---| | `clipShape(_:)` | A `RoundedRectangle` only; other shapes are marked unavailable | | `background(_:)` | A `Color` only; other shape styles are marked unavailable | | `shadow(color:radius:x:y:)` | A color, radius and offset | ## Availability on each release | Release | What it added for navigation transitions | |---|---| | iOS 18 | `navigationTransition(_:)`, `.automatic`, `.zoom(sourceID:in:)`, `matchedTransitionSource(id:in:)` and its configuration overload | | iOS 26 | `matchedTransitionSource(id:in:)` on toolbar content, so a toolbar item can be the zoom source | | iOS 27 | `.crossFade`, which presents a sheet by fading it in over the content | ## Supporting iOS 17 deployment targets Most apps still deploy back to iOS 17, where these modifiers do not exist. Guard them so older systems keep the standard push: - Wrap the call in a small `ViewModifier` whose `body` uses `if #available(iOS 18, *)` to apply `navigationTransition(_:)`, and returns the content unchanged otherwise. - Do the same for `matchedTransitionSource(id:in:)` on the source. - Keep both guards in step, so the source and the destination transition are applied on the same releases. ## Zoom versus matchedGeometryEffect - **matchedGeometryEffect** links the frames of two views inside one hierarchy when one is inserted and the other removed in the same animated change. It suits in-place heroes: an overlay that grows out of a grid in the same screen. - **The zoom navigation transition** is the navigation container's own presentation, aimed at a real destination pushed on a NavigationStack or shown as a sheet. You do not manage insertion, removal or sources by hand; the stack runs the transition. The choice follows the structure of the screen: a separate destination with its own place in the navigation path gets the zoom; an expanding overlay within the same view gets matched geometry. ## What to remember - Two modifiers, one id, one namespace: the source on the tapped view, the zoom on the destination root. - Put `navigationTransition(_:)` outside containers such as `VStack`. - The source configuration accepts only a rounded rectangle clip, a color background and a shadow. - Name the release: iOS 18 for the zoom, iOS 26 for toolbar sources, iOS 27 for the sheet cross-fade, and guard with `if #available` below that.

  • How do you keep this working for users still on iOS 17?
    Wrap each call in a small `ViewModifier` whose body applies `navigationTransition(_:)` or `matchedTransitionSource(id:in:)` inside `if #available(iOS 18, *)` and returns the content unchanged otherwise. On iOS 17 the push then uses the standard navigation transition.
  • When would you choose matchedGeometryEffect over the zoom navigation transition?
    When the expanded view is an overlay in the same screen rather than a navigation destination. matchedGeometryEffect links two views in one hierarchy changed in one animated transaction; the zoom is the navigation container's own presentation for a pushed view or a sheet.

  • navigationTransition goes on the NavigationLink label next to the source.
  • matchedGeometryEffect is the API for a zoom between a grid and a pushed destination.
  • The source configuration accepts any Shape for clipShape, such as Circle.
  • SwiftUI silently ignores navigationTransition on iOS 17, so no availability check is needed.
  • The zoom transition is available on macOS like the rest of NavigationStack.

Assumes the iOS 27 SDK: navigationTransition(_:), zoom(sourceID:in:) and matchedTransitionSource were added in iOS 18, toolbar sources in iOS 26 and crossFade in iOS 27.