8.5 · The layout negotiation, stacks, GeometryReader, safe area and custom Layout
SwiftUI layout is a conversation: a parent proposes a size, the child chooses its own, and the parent places it. Every layout surprise is a misreading of that conversation.
Mental model. Parent proposes, child chooses, parent places. A child can take less, more or exactly what is offered, and a parent must work with the answer.
The negotiation
Proposals, stacks, frames and padding.
- How does SwiftUI’s layout negotiation work, and why do frame and padding order change the result?
Layout is three steps repeated down the tree. The parent proposes a size to its child (possibly “unspecified”, “zero” or “infinity” to ask for the ideal…
Reading and bending layout
<code>GeometryReader</code>, safe areas and the <code>Layout</code> protocol.
- When do you use GeometryReader, and how do safe areas work in SwiftUI?
GeometryReader gives its content a GeometryProxy with the size and safe-area insets of the space it was offered, and lets you read frames in named coordinate… - How does the Layout protocol work, and when is a custom layout the right tool?
A type conforming to Layout takes over the negotiation for its subviews. You implement two required methods. sizeThatFits(proposal:subviews:cache:) returns the…
More in SwiftUI
- 8.1 · What a View is, and how SwiftUI knows which view is which
- 8.2 · The update loop, dependency tracking and the cost of body
- 8.3 · State, Binding, Environment and Observation
- 8.4 · onAppear, task, onChange and scenes
- 8.6 · Lists, ForEach and NavigationStack
- 8.7 · Animations, PhaseAnimator, Canvas and TimelineView
- 8.8 · Preference keys and coordinate spaces
- 8.9 · Previews and the macros behind SwiftUI
- 8.10 · State, bindings, Observation and environment, checked by running them
- 8.11 · The order of events in a render, and what Swift 6 isolation means for views
- 8.12 · Sharing space, alignment guides, adaptive containers and grids, measured
- 8.13 · What a ViewBuilder produces, and saving and restoring a navigation path