skip to content

Why does wrapping a view in SwiftUI's GeometryReader often break the surrounding layout, and how do you measure without that side effect?

level: middleimportance: must knowfreq 68%

answer

  1. who decides the reader's size
  2. flexible preferred size
  3. content never feeds back
  4. measure from behind, not around

basics

~20 s

GeometryReader reports a flexible size, so it takes all the space its parent offers and ignores its content's size. Wrapped around a view it bloats or squeezes the layout; put it in .background or .overlay so it adopts the measured view's size instead.

solid answer

~50 s

Apple's documentation says `GeometryReader` returns a flexible preferred size to its parent. In a `VStack` it therefore claims all the leftover height, the way a flexible view does, and its content has no say in that size: the content is built from the reader's size, not the other way round. Wrap a label in one and the label's row suddenly fills the screen, or, when little is offered, the content overflows a reader that never grew to fit it. The fix is to stop wrapping. Attach the reader inside `.background { }` or `.overlay { }` of the view you want to measure: those are laid out in the decorated view's frame, so the reader takes exactly that size and the layout is untouched. When you only need a number out of the geometry, `onGeometryChange(for:of:action:)` avoids the extra container altogether.

code

swift · 12 lines
swift
struct OrderSummary: View {
    var body: some View {
        VStack(spacing: 16) {
            GeometryReader { proxy in
                Text("Total: $129.00 (width \(Int(proxy.size.width)))")
            }
            .border(.red) // shows the reader filling the spare height
            Button("Place order") { }
        }
        .padding()
    }
}

go deeper

for a junior

Recall that GeometryReader takes all the space it is offered instead of hugging its content, and that measuring from .background or .overlay avoids the problem.

for a middle

Explain the negotiation: the reader returns a flexible size, its content cannot feed back, so wrapping a view changes both the row's size and the view's position. Show the background or overlay fix.

for a senior

Diagnose the screen quickly: a row that swells, content that overflows a small reader, a fixed frame added to hide it. Choose between a background reader, onGeometryChange and a layout tool that needs no measurement.

for a principal

Treat wrapping readers as a review smell for the team and steer layout intent towards containerRelativeFrame or a Layout, keeping GeometryReader for regions that genuinely fill their space.

## How SwiftUI sizes a GeometryReader SwiftUI lays views out by negotiation: a parent proposes a size, the child chooses its own size, and the parent places it. Most views choose a size from their content: a `Text` picks the width its string needs, a `VStack` sums its children. **`GeometryReader` is different by design.** Its content is a function of its size, so it cannot also derive its size from that content. Apple's documentation states the consequence in one line: the view **returns a flexible preferred size to its parent layout**. In practice that means: - The reader **accepts whatever is offered**. In a `VStack` with spare height it takes all of it, competing with other flexible views the way a `Spacer` or a `Color` does. - Its content **never feeds back**. A child that is taller than the reader does not make the reader taller; it simply extends past the reader's bounds. - The reader lays its children out inside its own frame, so wrapping a view also changes where that view sits; it is no longer positioned by the stack it used to live in. ## The symptoms in a real screen Wrapping is the usual mistake: a developer wants to know a label's width, so they put the label inside a `GeometryReader`. Typical results: 1. A short row **swells to fill the screen**, pushing the button below it to the bottom or off the visible area. 2. In a container that offers little space, the reader is **small and the content overflows** it, overlapping the next view. 3. Alignment breaks, because the measured view is now positioned by the reader instead of by its stack. 4. Developers then reach for a fixed `.frame(height:)` on the reader, which hides the symptom but hard-codes a size the content should decide, and fails again with Dynamic Type or a longer localized string. ## Measuring without changing the layout The robust pattern is to **measure from behind or in front of the view, not around it**. - **`.background { GeometryReader { ... } }` or `.overlay { GeometryReader { ... } }`.** A background or overlay is laid out in the frame of the view it decorates and does not change that view's size. A reader placed there is proposed exactly the decorated view's size, takes it (because it is flexible), and so measures the view precisely while the view keeps sizing itself from its content. - **`onGeometryChange(for:of:action:)`** (available from iOS 16). It attaches to the measured view, transforms the proxy into a small `Equatable` value, and runs an action when that value changes. No container is added, so nothing in the layout moves. - **Use a layout tool instead of measuring**, when the goal is layout rather than a number. Sizing a view as a fraction of its container is `containerRelativeFrame`, covered with size proposals; arranging children by custom rules is the `Layout` protocol, covered with custom containers. Both avoid a measure-then-set-state round trip. | Approach | Changes layout? | Gives you | |---|---|---| | Wrap the view in `GeometryReader` | Yes: reader is flexible | the space offered, not the view's size | | Reader inside `.background` / `.overlay` | No | the decorated view's exact size and frame | | `onGeometryChange(for:of:action:)` | No | a derived value, delivered on change | ## When the greedy size is exactly right A full-size `GeometryReader` is not a bug in itself. It is the right tool when a region is **meant to fill its space and derive its content from it**: a drawing surface, a custom gauge, a container that places children at fractions of the available width. Put it at the root of such a region. The defect is placing it deep inside content-sized rows, where its flexibility leaks into a layout that expected to hug its content. ## A checklist for code review - Is a `GeometryReader` **wrapping** a view whose size should come from its content? Move it into a background or overlay. - Is there a fixed `.frame` on a reader only to stop it from growing? That is usually the wrapping mistake in disguise. - Is the reader's closure **assigning state**? That is a side effect during a view update; move the read to `onGeometryChange` or to modifiers on a placeholder view. - Would `containerRelativeFrame` or a `Layout` express the intent without measuring at all?

  • Why isn't putting a fixed .frame(height:) on the reader a real fix?
    It stops the reader from claiming all the space, but it hard-codes a size the content should decide. The reader still never grows to fit its content, so with a larger Dynamic Type size or a longer localized string the content outgrows the fixed frame and overflows. Measuring from a background or overlay keeps the content in charge of its own size.
  • When is a full-size GeometryReader the right tool?
    When the region is meant to fill whatever space it gets and derive its content from that space: a drawing surface, a custom gauge, or a container that places children at fractions of the available width. Then the flexible size is what you want. Put the reader at the root of that region, not inside rows that should hug their content.
  • What is the lighter option when you only need a number from the geometry?
    `onGeometryChange(for:of:action:)`, available from iOS 16. It attaches to the measured view, transforms the `GeometryProxy` into an `Equatable` value, and runs the action when that value changes. It adds no container, so the layout is untouched, and returning a coarse value keeps the action rare.

  • GeometryReader sizes itself to fit its content like a VStack
  • Wrapping a view in GeometryReader is a harmless way to read its size
  • A fixed frame on the reader is the proper fix for the extra space
  • Putting the reader in a background changes the measured view's size
  • GeometryReader is always wrong and should never fill a region