In SwiftUI, a photo grid uses matchedGeometryEffect so a thumbnail grows into a full-screen photo, but the photo just pops in at full size; what usually causes that?
answer
- one pair, one transaction
- shared @Namespace, equal ids
- exactly one isSource view
- geometry linked, rendering is not
basics
~20 sUsually the views are not one matched pair in one animated change: separate @Namespace instances or unequal ids, an unanimated change, two inserted sources, or a detail pushed or shown as a sheet instead of swapped in place.
solid answer
~40 s`matchedGeometryEffect(id:in:)` links the geometry of views with the same id in the same namespace. When one view is inserted in the same transaction that another with that id is removed, SwiftUI interpolates their frames in window space. So check: the change is animated; both views use one `@Namespace` declared in a common ancestor and passed down as `Namespace.ID`; the ids are equal values; exactly one inserted view per id has `isSource: true`, since Apple documents anything else as undefined; and the detail is swapped in within the same hierarchy rather than pushed or presented as a sheet, where iOS 18's zoom navigation transition is the tool. Remember the modifier links geometry only, so raise the growing view with `zIndex(_:)` and use transitions for its look.
code
swift · 38 linesstruct Photo: Identifiable {
let id: Int
let assetName: String
}
struct PhotoWall: View {
let photos: [Photo]
@State private var selected: Photo?
@Namespace private var hero
var body: some View {
ZStack {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 100))]) {
ForEach(photos) { photo in
if selected?.id == photo.id {
Color.clear.frame(height: 100)
} else {
Image(photo.assetName)
.resizable()
.scaledToFit()
.matchedGeometryEffect(id: photo.id, in: hero)
.frame(height: 100)
.onTapGesture { selected = photo }
}
}
}
if let photo = selected {
Image(photo.assetName)
.resizable()
.scaledToFit()
.matchedGeometryEffect(id: photo.id, in: hero)
.zIndex(1)
.onTapGesture { selected = nil }
}
}
.animation(.snappy, value: selected?.id)
}
}go deeper
Recall that matchedGeometryEffect needs an id and a namespace from @Namespace, and that both views use the same pair.
Explain the documented mechanism: a view inserted in the same transaction another with the same id is removed gets its frame interpolated in window space, and isSource names the geometry provider.
Debug a pop systematically: animation, shared namespace, equal ids, exactly one source, same transaction and hierarchy. Fix z-order with zIndex and know when a navigation zoom is the better tool.
Decide where hero motion is worth its cost: an in-place overlay hero couples grid and detail into one view, so weigh that coupling against the system zoom navigation transition when the detail is a real destination.
## What matchedGeometryEffect does `matchedGeometryEffect(id:in:properties:anchor:isSource:)` links the **geometry** of views that share an identifier inside a namespace. Its full signature has three defaulted parameters: `properties` (default `.frame`), `anchor` (default `.center`) and `isSource` (default `true`). The namespace comes from a property declared with **`@Namespace`**, a dynamic property whose value is tied to the persistent identity of the view that declares it. Apple's documentation describes two things the modifier does: - It sets the geometry of each view in the group from the **inserted view with `isSource = true`**, the source, updating the values named by `properties` (`.frame`, `.position` or `.size`). - If a view is inserted **in the same transaction** that another view with the same key is removed, SwiftUI **interpolates their frame rectangles in window space**, so the two look like a single view moving from the old place to the new one. It also states the limit: the modifier only links geometry, not rendering. How each of the two views looks while they swap, fading, scaling and so on, comes from their ordinary transitions. ## The hero pattern for a photo grid The classic use is a thumbnail that grows into a full-screen photo. In one view hierarchy, the grid shows a thumbnail for each photo; when a photo is selected, its thumbnail is removed (a placeholder keeps the grid's layout) and a large copy is inserted in an overlay. Both carry `.matchedGeometryEffect(id: photo.id, in: hero)`, and the selection changes inside an animation. Because exactly one of the two is in the hierarchy at any time, each is the sole source in its own state, and SwiftUI interpolates between their frames. ```swift struct Photo: Identifiable { let id: Int let assetName: String } struct PhotoWall: View { let photos: [Photo] @State private var selected: Photo? @Namespace private var hero var body: some View { ZStack { LazyVGrid(columns: [GridItem(.adaptive(minimum: 100))]) { ForEach(photos) { photo in if selected?.id == photo.id { Color.clear.frame(height: 100) } else { Image(photo.assetName) .resizable() .scaledToFit() .matchedGeometryEffect(id: photo.id, in: hero) .frame(height: 100) .onTapGesture { selected = photo } } } } if let photo = selected { Image(photo.assetName) .resizable() .scaledToFit() .matchedGeometryEffect(id: photo.id, in: hero) .zIndex(1) .onTapGesture { selected = nil } } } .animation(.snappy, value: selected?.id) } } ``` ## Why it pops instead of growing When the large photo simply appears at full size, one of these is usually the cause: 1. **The change is not animated.** Matching views still need an animated transaction; without `withAnimation` or `animation(_:value:)` on a container present in both states, SwiftUI jumps straight to the new layout. 2. **The views are in different namespaces.** If the grid view and the detail view are separate structs and **each declares its own `@Namespace`**, their IDs live in different namespaces and never match. Declare the namespace once in a common ancestor and pass its `Namespace.ID` down as a parameter. 3. **The identifiers differ.** The grid uses the array index while the detail uses `photo.id`, or one side uses an `Int` and the other a `String`. The IDs must be equal `Hashable` values. 4. **There is not exactly one source.** If the thumbnail stays on screen with the default `isSource: true` while the detail is also inserted with `isSource: true`, there are two sources; the documentation says that when the number of inserted source views is not exactly one, the results are undefined. Either remove the thumbnail in the same change, as above, or mark one of the two `isSource: false` so it takes the other's geometry. 5. **The detail is not swapped in the same hierarchy and transaction.** A detail inserted later, after an asynchronous load for example, misses the transaction in which the thumbnail was removed. A detail presented by a NavigationStack push or a sheet is placed by that container's own presentation; for that case iOS 18 added `navigationTransition(.zoom(sourceID:in:))` with `matchedTransitionSource(id:in:)`. ## Using isSource and properties deliberately - **`isSource: false`** suits views that both stay on screen, such as a selection highlight that should follow whichever item is selected: the selected item is the source, the highlight takes its frame. - **`properties: .position`** matches only position, leaving each view to keep its own size; **`.size`** does the opposite. - **`anchor`** chooses which point of each view is used for the shared position value. ## Rendering and order Because the modifier links geometry only, the look of the swap is up to you: - Give each view a transition if the default look during the swap is not what you want. - Raise the growing view with `zIndex(_:)` so it draws above the grid while it moves. - Use `scaledToFit()` on both images so the interpolated frames correspond to the same aspect ratio. ## How to debug one Start from the documentation's own conditions and check them in order: one shared namespace, equal IDs, one inserted source per ID, insertion and removal in one animated transaction, and both views in the same hierarchy. Each failure has the same symptom, a pop instead of a glide, so checking them one at a time is quicker than tweaking animation curves.
- When would you set isSource to false instead of removing the thumbnail?When both views must stay on screen, such as a highlight that follows the selected item in a list. The selected item keeps `isSource: true` and the highlight uses `isSource: false`, so it takes the item's frame and glides when the selection changes. The rule is still exactly one inserted source per id.
- The detail view lives in another struct; how do you share the namespace?Declare `@Namespace` once in the view that owns both, and pass its value down as a `Namespace.ID` parameter to the child views. A second `@Namespace` in the child creates a different namespace whose ids never match the parent's.
- Why does the growing photo sometimes slide underneath the grid?matchedGeometryEffect links frames, not drawing order. In a ZStack the photo is drawn according to its front-to-back order, so give it a higher `zIndex(_:)` to keep it above the grid during the animation.
Two actors play the same character in different scenes, and the edit makes them look like one person crossing the room. The match only works if they share the role name in the same film, and only one of them is on camera as that character at a time; costumes stay each actor's own.
- Leaving both views inserted with the default isSource is fine; SwiftUI picks one.
- Each view can declare its own @Namespace as long as the ids match.
- matchedGeometryEffect also animates the image content and opacity between the two views.
- It glides between a grid and a pushed NavigationStack destination just like an in-place swap.
- The id must be a String for views to be matched.