# matchedGeometryEffect in SwiftUI

**Full code:** [**https://github.com/xavier7t/iOSDevX/blob/main/iOSDevX/202611-Nov 2026/DemoMatchedGeometryEffect20261122.swift**](https://github.com/xavier7t/iOSDevX/blob/main/iOSDevX/202611-Nov%202026/DemoMatchedGeometryEffect20261122.swift)

`matchedGeometryEffect` lets SwiftUI treat two different views as the same element, so it can animate the position and size from one to the other. It is the easiest way to build a hero-style transition, like a card in a grid that grows into a detail view. In this post we build exactly that with a few lines of code. It works on iOS 14 and later.

## Why use matchedGeometryEffect?

1. **Smooth transitions with little code.** You describe the start and end layouts. SwiftUI animates the change for you.
2. **No manual frame math.** You don't need `GeometryReader` or custom offsets to move a view between two places.
3. **Works with any view.** Shapes, images, text and whole stacks can all be matched.

## Step-by-step implementation

### Step 1: Create a model

Each card needs a stable `id`. SwiftUI uses it to know which views are the same element.

```swift
struct Card: Identifiable {
    let id = UUID()
    let title: String
    let symbol: String
    let color: Color
}
```

### Step 2: Add a namespace and a selection

A `@Namespace` groups the views that can be matched. `selected` decides whether we show the grid or the detail.

```swift
@Namespace private var namespace
@State private var selected: Card?
```

The body swaps between the two layouts:

```swift
var body: some View {
    ZStack {
        if let card = selected {
            detail(for: card)
        } else {
            grid
        }
    }
    .padding()
}
```

### Step 3: Build the grid

Each tile calls `matchedGeometryEffect(id:in:)` with the card's `id` and the shared namespace. Tapping a tile sets `selected` inside `withAnimation`.

```swift
private var grid: some View {
    LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 16) {
        ForEach(cards) { card in
            RoundedRectangle(cornerRadius: 20)
                .fill(card.color)
                .matchedGeometryEffect(id: card.id, in: namespace)
                .frame(height: 120)
                .overlay {
                    Image(systemName: card.symbol)
                        .font(.largeTitle)
                        .foregroundColor(.white)
                        .matchedGeometryEffect(id: "\(card.id)-symbol", in: namespace)
                }
                .onTapGesture {
                    withAnimation(.spring(response: 0.5, dampingFraction: 0.8)) {
                        selected = card
                    }
                }
        }
    }
}
```

Note that `matchedGeometryEffect` comes before `.frame`. The modifier needs to wrap the view before its size is fixed, so SwiftUI can interpolate the size.

### Step 4: Build the detail view

The detail view uses the same IDs and the same namespace, but a bigger frame. SwiftUI sees matching IDs and animates between the two.

```swift
private func detail(for card: Card) -> some View {
    VStack(spacing: 16) {
        RoundedRectangle(cornerRadius: 20)
            .fill(card.color)
            .matchedGeometryEffect(id: card.id, in: namespace)
            .frame(height: 300)
            .overlay {
                Image(systemName: card.symbol)
                    .font(.system(size: 80))
                    .foregroundColor(.white)
                    .matchedGeometryEffect(id: "\(card.id)-symbol", in: namespace)
            }

        Text(card.title)
            .font(.largeTitle.bold())

        Spacer()
    }
    .onTapGesture {
        withAnimation(.spring(response: 0.5, dampingFraction: 0.8)) {
            selected = nil
        }
    }
}
```

## Example usage

Put the view on screen, tap a tile to expand it, and tap again to go back.

```swift
#Preview {
    DemoMatchedGeometryEffect.ContentView()
}
```

## Key components explained

1. `@Namespace`
   - Creates a scope for matching. Only views in the same namespace can be matched with each other.
2. `matchedGeometryEffect(id:in:)`
   - Views that share an `id` in the same namespace are treated as one element.
   - Only one of the matched views should be on screen at a time. That is why we use `if/else` instead of showing both.
3. `withAnimation`
   - The animation starts when state changes inside it. Without it, the views would swap instantly.
4. **Separate IDs for the symbol**
   - The icon has its own ID, so it animates on its own and scales with the card.

## Benefits

- **Polished feel.** Hero transitions make your app feel more connected.
- **Less code.** One modifier replaces custom geometry and offset logic.

## Conclusion

`matchedGeometryEffect` is a small modifier with a big visual payoff. Give two views the same ID in one namespace, change state inside `withAnimation`, and SwiftUI does the rest. Try it on your own lists, tab indicators and detail screens.

*If you found this article helpful, please consider supporting my work on* [*Buy Me a Coffee*](https://buymeacoffee.com/xavierios)*. Don't forget to subscribe to my newsletter for more tutorials like this!*
