In SwiftUI, what does a GeometryReader's GeometryProxy let you read, and what do local, global and named coordinate spaces mean?
answer
- a proxy, not the view
- size, frame, insets, anchors
- same rectangle, different origin
- an ancestor tagged by name
basics
~20 sA GeometryProxy reports the reader's size, its frame(in:) a chosen coordinate space, its safeAreaInsets, and resolves anchors. .local measures from the view itself, .global from the root of the hierarchy, and .named(:) from an ancestor tagged with coordinateSpace(:).
solid answer
~40 s`GeometryReader` passes its content closure a `GeometryProxy`. From it you read `size` (the space the reader was given), `frame(in:)` (that rectangle expressed in a coordinate space you choose), `safeAreaInsets`, and you resolve an `Anchor` with the proxy's subscript. The coordinate space only moves the origin of the numbers: `.local` measures from the reader's own top-leading corner, so the origin is zero; `.global` measures from the root of the view hierarchy; and `.named("card")` measures from the ancestor you tagged with `.coordinateSpace(.named("card"))`. A named space is the robust choice for position inside a component, because global positions shift whenever bars, sheets or the window change. Since iOS 17 these are `CoordinateSpaceProtocol` values; the older `coordinateSpace(name:)` and the `frame(in:)` overload that takes a `CoordinateSpace` are marked for deprecation.
code
swift · 21 linesstruct ProfileCard: View {
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Ada Lovelace")
.font(.title2)
GeometryReader { proxy in
let inCard = proxy.frame(in: .named("card"))
let inRoot = proxy.frame(in: .global)
VStack(alignment: .leading) {
Text("size: \(Int(proxy.size.width)) x \(Int(proxy.size.height))")
Text("y in card: \(Int(inCard.minY))")
Text("y global: \(Int(inRoot.minY))")
}
.font(.caption.monospaced())
}
.frame(height: 60)
}
.padding()
.coordinateSpace(.named("card"))
}
}go deeper
Recall the proxy's members: size, frame(in:), safeAreaInsets and the anchor subscript. Name the three coordinate spaces and say where each one's origin is.
Explain that frame(in:) converts one rectangle between frames of reference, why local always starts at zero, and how coordinateSpace(_:) with a named space lets a nested view measure inside its container.
Show the judgment: prefer a named space over global for anything inside a reusable component, read the proxy only inside the closure, and write the iOS 17 CoordinateSpaceProtocol forms rather than the overloads marked for deprecation.
Little to decide here beyond a convention: a team that names container spaces consistently gets geometry code that survives being moved between screens, which is the angle worth raising in a code review.
## What GeometryReader hands you `GeometryReader` is a container view whose content is built **as a function of its own size and coordinate space**. Its initializer takes a closure, and SwiftUI calls that closure with a **`GeometryProxy`**: a read-only window onto the geometry SwiftUI has already settled for the reader. The proxy is not the view and not a stored measurement. It is valid inside the closure while SwiftUI builds the content, so you read what you need there and turn it into layout or drawing. Because the reader's content depends on its size, the reader cannot also size itself to that content. Apple's documentation puts it plainly: the view returns a flexible preferred size to its parent layout. That greedy sizing is a separate interview question; here the subject is what the proxy tells you. ## The four things a GeometryProxy answers - **`size`**: the width and height SwiftUI gave the reader. This is the space you lay content out in. - **`frame(in:)`**: the reader's rectangle, expressed in a coordinate space you pass. The size part is the same in every space; the origin changes. - **`safeAreaInsets`**: the safe-area insets that apply to the reader, as an `EdgeInsets` value, useful when content should run under a bar but keep text out of it. - **The anchor subscript, `proxy[anchor]`**: converts an `Anchor` value (produced by `anchorPreference`) into a concrete `CGRect` or `CGPoint` in the reader's own space. The proxy has a few more members, such as `bounds(of:)` for a named space (iOS 17), but the four above are the ones every SwiftUI developer is expected to know. ## Coordinate spaces: the same rectangle, a different origin A coordinate space is a **frame of reference**. Every geometric property of a view is defined in its parent's local space, and `frame(in:)` converts it into another one. | Space | Written as | Origin of the numbers | Typical use | |---|---|---|---| | Local | `.local` | the reader's own top-leading corner | drawing inside the reader; origin is always zero | | Global | `.global` | the root of the view hierarchy | rough on-screen position, debugging | | Named | `.named("card")` | the ancestor tagged with that name | position inside a container you control | Two consequences matter in practice: 1. A **global** frame bakes in everything above your view: navigation bars, a sheet's offset, a split view's sidebar, the window size on iPad. The same component reports different numbers in a different context. 2. A **named** frame measures only inside the container you tagged, so the numbers mean the same thing wherever the component is used. That is why parallax, sticky and alignment tricks inside a component should read a named space. On iPad, where a window can be resized or shown beside another app, the gap between the two grows further. ## Naming a space with coordinateSpace(_:) Setting up a named space takes two steps: 1. Attach `.coordinateSpace(.named("card"))` to the ancestor you want to measure from. Any `Hashable` value works as the name. 2. Inside a descendant's `GeometryReader`, call `proxy.frame(in: .named("card"))`. The same coordinate-space values are accepted elsewhere in SwiftUI. `DragGesture(coordinateSpace:)` reports locations in the space you pass, and `onTapGesture(count:coordinateSpace:perform:)` reports the tap location the same way, so a gesture and a geometry read can share one frame of reference. ## Version notes and pitfalls - **API shape since iOS 17.** `coordinateSpace(_:)` takes a `NamedCoordinateSpace`, and `frame(in:)` accepts any `CoordinateSpaceProtocol` value (`.local`, `.global`, `.named(_:)`). The older `coordinateSpace(name:)` and the `frame(in:)` overload that takes the `CoordinateSpace` enum are marked for deprecation in the iOS 27 SDK, with messages pointing at the new forms. Since iOS 17 is the usual deployment floor, write the new forms. - **Keep values, not the proxy.** Read what you need inside the closure and keep plain values such as a `CGFloat` or `CGRect`; `GeometryProxy` is not `Sendable` or `Equatable`, so it is the wrong thing to store or compare. - **Local is not "position in parent".** `frame(in: .local)` always starts at zero; to know where the view sits inside a container, name the container. - **Global moves.** Scrolling, a keyboard, a bar appearing or the window resizing all change a global frame. - **Scroll views have their own space.** Scroll-specific geometry and visibility callbacks are covered with scroll views; here the point is only that a named space works inside them like anywhere else.
- Why is a named coordinate space usually safer than .global for positioning work inside a component?A global frame includes everything above the component: navigation bars, sheets, sidebars, the window size. Put the same component in another screen and its global numbers change, so thresholds tuned on one screen break on the next. A named space set on the component's own container measures only inside it, so the numbers mean the same thing wherever the component appears.
- Which other SwiftUI APIs take the same coordinate-space values as frame(in:)?Gestures do. `DragGesture(coordinateSpace:)` reports its locations in the space you pass, and `onTapGesture(count:coordinateSpace:perform:)` reports the tap location the same way. Tagging a container with `.coordinateSpace(.named("board"))` lets a drag and a geometry read agree on one frame of reference without converting between spaces by hand.
- frame(in:) reports a different size in each coordinate space
- frame(in: .local) tells you where the view sits inside its parent
- A named coordinate space is declared with .id() on the container
- Global coordinates stay the same when a bar appears or content scrolls
- coordinateSpace(name:) with a String is the current API on iOS 17