SwiftUI Layout & Containers
The layer between "stacks and spacers" and "it scrolls like butter with 100k rows" — Apple's
Layout protocol, container composition, and the lazy-stack rules from the WWDC26 deep dive.
View identity/data-flow questions route to swiftui/data-flow.
When This Skill Activates
- "Make these buttons equal width" / measurement-dependent layout
- Building a reusable container (custom List/board/carousel) that should accept ForEach + sections
- Lazy stack jank, memory growth, scroll-position bugs, broken scroll targeting
- Programmatic scrolling, paging/snapping, scroll-linked effects
- GeometryReader causing layout loops or mangled sizing
Custom Layout protocol (not GeometryReader)
Reach for a custom Layout whenever you must measure subviews and feed the measurement back
into layout — GeometryReader only measures its container and can't influence the engine.
Canonical case: equal-width buttons.
sizeThatFits: propose .unspecified to read each subview's ideal size
(subviews.map { $0.sizeThatFits(.unspecified) }); guard empty subviews;
replacingUnspecifiedDimensions() for nil proposal dimensions.
placeSubviews: never assume origin (0,0) — use bounds.minX/midX (non-zero origins are
what make layouts composable); place(at:anchor:proposal:) with a proposal that may differ
from the ideal size (that's how equal widths happen).
- Respect spacing preferences:
subviews[i].spacing.distance(to:along:), taking the larger
of conflicting preferences — matching built-in containers. No hardcoded 8s.
- Per-subview data via
LayoutValueKey (+ a layoutValue convenience modifier), read as
subview[Key.self].
- Cache only after Instruments shows layout cost — it's an optimization, not a requirement.
- Switch layouts without killing identity:
AnyLayout(HStackLayout()) ↔ custom layout with
.animation(_:value:) — SwiftUI sees one changing view, so state survives and it animates.
- Don't build fallbacks into the layout — wrap alternatives in
ViewThatFits.
Grid decisions
| Need |
Use |
| Static 2D with cross-row alignment |
Grid/GridRow (+ gridCellColumns to span, gridColumnAlignment per column) |
| Scrollable, large content |
LazyVGrid/LazyHGrid (only visible views load; one axis fixed up front) |
| "First arrangement that fits" |
ViewThatFits |
Custom containers (Demystify Containers)
Make containers that compose like List does:
- API shape: a trailing
@ViewBuilder var content: Content — callers can then mix static
views, ForEach, and conditionals.
- Iterate resolved children with
ForEach(subviews: content); need the whole collection
(count/chunking)? Group(subviews: content) { subviews in … }.
- Internalize declared vs resolved: one declared ForEach resolves to N subviews; Group to
its children; EmptyView to zero;
if conditionally. Counting declared views is a bug.
- Sections are opt-in:
ForEach(sections: content), reading section.header /
section.content; check header.isEmpty before rendering the slot.
- Per-child customization via container values:
extension ContainerValues { @Entry var … },
set with a convenience modifier, read via subview.containerValues. Scoping model:
Environment flows down · Preferences flow up · container values reach only the direct
container. Setting one on a Section styles the whole section.
Lazy stacks & scrolling performance (WWDC26 rules)
LazyVStack builds views only until the viewport fills; totals and offsets are estimated
from average placed-view size and corrected as you scroll. Everything below follows from that:
- One subview per ForEach element, always. An
if inside a row (0-or-1 views) forces the
stack to keep off-screen views + their @State alive to preserve indices — and environment
changes then re-evaluate off-screen bodies. Filter at the data layer (@Query predicate);
gate auth-type conditions outside the stack.
- Never key logic off absolute scroll offset in a lazy stack (
onScrollGeometryChange sees
estimates) — use onScrollTargetVisibilityChange(threshold: 0.8) for visibility triggers.
- Set up in
init, not onAppear (_model = State(initialValue:)): body runs during
prefetch; onAppear fires only on-screen, throwing prefetch work away and causing
post-appearance size jumps. Start async loads in init/task.
- Don't persist meaningful state in row
@State — off-screen views are eventually
released. Hoist (@State var highlighted: Set<ID> outside, @Binding down).
scrollTransition transforms must stay inside the original frame (scale ✅; rotations
escaping the frame make views vanish early).
- Don't drive layout from
onGeometryChange height feedback (content shoves, targeting
breaks) — that's the custom Layout case above.
- Nest
LazyHStack inside LazyVStack freely (unscrolled rows stay unloaded) — but fix child
heights (lineLimit, explicit frames) in the horizontal stacks.
pinnedViews: [.sectionHeaders] pins headers; infinite scroll = trailing
ProgressView().onAppear { fetchNextPage() } after the ForEach.
The scroll API map
- Snapping/paging:
scrollTargetLayout() + scrollTargetBehavior(.viewAligned/.paging).
- Track/control position:
scrollPosition binding; programmatic ScrollPosition +
scrollTo(id:) — works for unloaded targets if IDs map to stable one-subview elements.
- Scroll-linked effects:
scrollTransition (enter/leave viewport) and visualEffect
(geometry without GeometryReader) — details in design/animation-patterns.
- Reactions:
onScrollGeometryChange (fine outside lazy estimation),
onScrollVisibilityChange (autoplay/analytics).
- Performance floor: list/scroll internals were rewritten (WWDC25) — macOS lists ~6× faster at
100k+ rows, and lazy loading works in nested
ScrollView+LazyVStack; profile with the
SwiftUI instrument (performance/swiftui-debugging).
Output Format
Layout review: Symptom | Rule violated | Fix — check the one-subview-per-element rule first
in any lazy-stack complaint; it explains most jank, memory growth, and targeting bugs.
References
1---2name: layout3description: SwiftUI layout beyond stacks — the Layout protocol (when custom layout beats GeometryReader), Grid vs lazy grids, custom containers with sections and container values, and lazy-stack/ScrollView performance rules (what breaks laziness, prefetch discipline, scroll APIs). Use when building custom layouts or containers, fixing lazy-stack jank or memory growth, or wiring programmatic/snapping scrolling.4---5
6# SwiftUI Layout & Containers
7
8The layer between "stacks and spacers" and "it scrolls like butter with 100k rows" — Apple's
9Layout protocol, container composition, and the lazy-stack rules from the WWDC26 deep dive.
10View identity/data-flow questions route to `swiftui/data-flow`.
11
12## When This Skill Activates
13
14- "Make these buttons equal width" / measurement-dependent layout
15- Building a reusable container (custom List/board/carousel) that should accept ForEach + sections
16- Lazy stack jank, memory growth, scroll-position bugs, broken scroll targeting
17- Programmatic scrolling, paging/snapping, scroll-linked effects
18- GeometryReader causing layout loops or mangled sizing
19
20## Custom Layout protocol (not GeometryReader)
21
22Reach for a custom `Layout` whenever you must **measure subviews and feed the measurement back
23into layout** — GeometryReader only measures its container and can't influence the engine.
24Canonical case: equal-width buttons.
25
26- `sizeThatFits`: propose `.unspecified` to read each subview's ideal size
27 (`subviews.map { $0.sizeThatFits(.unspecified) }`); guard empty subviews;
28 `replacingUnspecifiedDimensions()` for nil proposal dimensions.
29- `placeSubviews`: never assume origin (0,0) — use `bounds.minX/midX` (non-zero origins are
30 what make layouts composable); `place(at:anchor:proposal:)` with a proposal that may differ
31 from the ideal size (that's how equal widths happen).
32- **Respect spacing preferences**: `subviews[i].spacing.distance(to:along:)`, taking the larger
33 of conflicting preferences — matching built-in containers. No hardcoded 8s.
34- Per-subview data via `LayoutValueKey` (+ a `layoutValue` convenience modifier), read as
35 `subview[Key.self]`.
36- Cache only after Instruments shows layout cost — it's an optimization, not a requirement.
37- **Switch layouts without killing identity**: `AnyLayout(HStackLayout())` ↔ custom layout with
38 `.animation(_:value:)` — SwiftUI sees one changing view, so state survives and it animates.
39- Don't build fallbacks into the layout — wrap alternatives in `ViewThatFits`.
40
41## Grid decisions
42
43| Need | Use |
44|---|---|
45| Static 2D with cross-row alignment | `Grid`/`GridRow` (+ `gridCellColumns` to span, `gridColumnAlignment` per column) |
46| Scrollable, large content | `LazyVGrid`/`LazyHGrid` (only visible views load; one axis fixed up front) |
47| "First arrangement that fits" | `ViewThatFits` |
48
49## Custom containers (Demystify Containers)
50
51Make containers that compose like `List` does:
52
53- API shape: a trailing `@ViewBuilder var content: Content` — callers can then mix static
54 views, `ForEach`, and conditionals.
55- Iterate **resolved** children with `ForEach(subviews: content)`; need the whole collection
56 (count/chunking)? `Group(subviews: content) { subviews in … }`.
57- Internalize **declared vs resolved**: one declared ForEach resolves to N subviews; Group to
58 its children; EmptyView to zero; `if` conditionally. Counting declared views is a bug.
59- Sections are opt-in: `ForEach(sections: content)`, reading `section.header` /
60 `section.content`; check `header.isEmpty` before rendering the slot.
61- Per-child customization via container values: `extension ContainerValues { @Entry var … }`,
62 set with a convenience modifier, read via `subview.containerValues`. Scoping model:
63 **Environment flows down · Preferences flow up · container values reach only the direct
64 container.** Setting one on a `Section` styles the whole section.
65
66## Lazy stacks & scrolling performance (WWDC26 rules)
67
68LazyVStack builds views only until the viewport fills; totals and offsets are **estimated**
69from average placed-view size and corrected as you scroll. Everything below follows from that:
70
71- **One subview per ForEach element, always.** An `if` inside a row (0-or-1 views) forces the
72 stack to keep off-screen views + their `@State` alive to preserve indices — and environment
73 changes then re-evaluate off-screen bodies. Filter at the data layer (`@Query` predicate);
74 gate auth-type conditions *outside* the stack.
75- **Never key logic off absolute scroll offset** in a lazy stack (`onScrollGeometryChange` sees
76 estimates) — use `onScrollTargetVisibilityChange(threshold: 0.8)` for visibility triggers.
77- **Set up in `init`, not `onAppear`** (`_model = State(initialValue:)`): body runs during
78 prefetch; `onAppear` fires only on-screen, throwing prefetch work away and causing
79 post-appearance size jumps. Start async loads in `init`/`task`.
80- **Don't persist meaningful state in row `@State`** — off-screen views are eventually
81 released. Hoist (`@State var highlighted: Set<ID>` outside, `@Binding` down).
82- `scrollTransition` transforms must stay inside the original frame (scale ✅; rotations
83 escaping the frame make views vanish early).
84- Don't drive layout from `onGeometryChange` height feedback (content shoves, targeting
85 breaks) — that's the custom `Layout` case above.
86- Nest `LazyHStack` inside `LazyVStack` freely (unscrolled rows stay unloaded) — but fix child
87 heights (`lineLimit`, explicit frames) in the horizontal stacks.
88- `pinnedViews: [.sectionHeaders]` pins headers; infinite scroll = trailing
89 `ProgressView().onAppear { fetchNextPage() }` after the ForEach.
90
91## The scroll API map
92
93- Snapping/paging: `scrollTargetLayout()` + `scrollTargetBehavior(.viewAligned/.paging)`.
94- Track/control position: `scrollPosition` binding; programmatic `ScrollPosition` +
95 `scrollTo(id:)` — works for unloaded targets *if* IDs map to stable one-subview elements.
96- Scroll-linked effects: `scrollTransition` (enter/leave viewport) and `visualEffect`
97 (geometry without GeometryReader) — details in `design/animation-patterns`.
98- Reactions: `onScrollGeometryChange` (fine outside lazy estimation),
99 `onScrollVisibilityChange` (autoplay/analytics).
100- Performance floor: list/scroll internals were rewritten (WWDC25) — macOS lists ~6× faster at
101 100k+ rows, and lazy loading works in nested `ScrollView`+`LazyVStack`; profile with the
102 SwiftUI instrument (`performance/swiftui-debugging`).
103
104## Output Format
105
106Layout review: `Symptom | Rule violated | Fix` — check the one-subview-per-element rule first
107in any lazy-stack complaint; it explains most jank, memory growth, and targeting bugs.
108
109## References
110
111- https://developer.apple.com/videos/play/wwdc2022/10056/ (Compose custom layouts)
112- https://developer.apple.com/videos/play/wwdc2024/10146/ (Demystify SwiftUI containers)
113- https://developer.apple.com/videos/play/wwdc2026/321/ (Dive into lazy stacks and scrolling)
114- Related skills: `swiftui/data-flow` (identity/ForEach IDs), `performance/swiftui-debugging`, `design/animation-patterns` (scroll-linked effects)