8.12 · Sharing space, alignment guides, adaptive containers and grids, measured
The negotiation rules are easy to recite and easy to mispredict. These layouts were hosted in an iOS Simulator and measured with GeometryReader, so the numbers are what SwiftUI actually produced.
Mental model. Every container proposes, every child answers. Flexible children get what is left after rigid ones; guides move whole stacks; adaptive containers decide by proposal, not by content you cannot see.
Stacks and guides
How an HStack divides a tight width, and how alignment guides shift siblings.
- How does an HStack divide a width that is too small, and what do layoutPriority and fixedSize change?
The stack asks each child what size it would take for the proposal, sorts children by flexibility (how much their size can vary) and offers space to the least… - How do alignment guides and custom alignments work, and why does moving one view’s guide move its siblings?
A stack with an alignment (.leading, .center, …) lines each child up at the position of that alignment's guide, a number each child reports: by default the…
Adaptive containers
ViewThatFits, AnyLayout and the grid types.
- How do ViewThatFits and AnyLayout differ, and which one preserves state when the arrangement changes?
ViewThatFits picks the first child whose ideal size fits the proposal along the constrained axes, falling back to the last. In the check, in 300 points it… - How do GridItem sizes (fixed, flexible, adaptive) and Grid differ in how they size columns?
LazyVGrid takes an array of GridItem column descriptions and lays children into them row by row. .flexible() columns share the width left over (two of them in…
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.5 · The layout negotiation, stacks, GeometryReader, safe area and custom Layout
- 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.13 · What a ViewBuilder produces, and saving and restoring a navigation path