In SwiftUI, how does alignmentGuide(_:computeValue:) shift one child of a stack, and why does it sometimes have no visible effect?
answer
- a position, not a distance
- measured in the view's own coordinates
- must match the stack's alignment
- HStack reads vertical guides only
basics
~20 salignmentGuide(_:computeValue:) overrides where one view reports a guide, in its own coordinates, and the stack moves that view to keep the guides aligned. It has an effect only when that guide is the container's active alignment.
solid answer
~40 sThe closure receives `ViewDimensions` and returns where the guide sits inside the view, not how far to move it. In `VStack(alignment: .leading)`, returning `d[.leading] - 16` puts the child's leading guide 16 points before its edge, so the stack shifts the child 16 points toward trailing: an indent without another stack. The override is ignored when the container aligns by a different guide, for example a default `VStack` aligned by `.center`, or an `HStack`, which only reads vertical guides; Apple notes that SwiftUI doesn't invoke the closure unless the guide is active. The shifted child can enlarge the stack and move its siblings relative to it, but the child's own size is unchanged, unlike `padding`.
code
swift · 11 linesstruct ReceiptLines: View {
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("Latte")
Text("Oat milk")
.font(.caption)
.alignmentGuide(.leading) { d in d[.leading] - 16 }
Text("Croissant")
}
}
}go deeper
Recall that alignmentGuide adjusts one child's alignment inside a stack and that the guide you override has to match the stack's alignment.
Explain that the closure returns a guide position in the view's own coordinates, and work out which way a returned value moves the view.
Diagnose an override that does nothing by checking the active guide and axis, and weigh it against padding or offset for the layout you need.
Judge when per-view guide tweaks signal a structural problem that a shared row component or a different container should solve instead.
## What an alignment guide value is Every view has **alignment guides**: named positions such as `.leading`, `.center` or `.firstTextBaseline`, each measured as an offset from the view's own origin. A stack lines its children up by bringing the same guide of each child onto one line. `ViewDimensions`, the value your closure receives, gives you the view's `width` and `height` and lets you read any guide with a subscript, `d[.leading]` or `d[.bottom]`. `alignmentGuide(_:computeValue:)` overrides where one view reports a guide. Its signature takes the guide (a `HorizontalAlignment` or a `VerticalAlignment` overload) and a closure from `ViewDimensions` to `CGFloat`. The number you return is the **guide's position inside the view**, not a distance to move. The stack then moves the view so that position meets the shared line. ## Shifting one child in a leading-aligned VStack Take `VStack(alignment: .leading)` and override `.leading` on one child: | Closure returns | The child's guide sits | Visible result | |---|---|---| | `d[.leading]` | on its leading edge | nothing changes | | `d[.leading] - 16` | 16 points before its leading edge | the child moves 16 points toward trailing, an indent | | `d[.trailing]` | on its trailing edge | the child hangs out toward the leading side | | `d.width / 2` | at its middle | the child is centered on the others' leading edge | The second row is the classic receipt pattern: a modifier line such as "Oat milk" under "Latte" is indented without wrapping it in another stack. The guide math stays the same in right-to-left layouts; Apple's `ViewDimensions` documentation explains that SwiftUI moves the origin to the right and inverts the x direction, so the indent mirrors automatically. ## Why it sometimes does nothing Apple's stack-alignment article is explicit: when you use an alignment guide modifier, make sure it names an active alignment of the stack, otherwise SwiftUI doesn't invoke the closure. The usual causes: 1. **The stack uses another guide.** A `VStack` created with default arguments aligns by `.center`, so a `.leading` override is never consulted. 2. **The axis does not match.** An `HStack` aligns its children with a `VerticalAlignment`; a horizontal override such as `.leading` on its child is not used by that `HStack`. It can still matter to an enclosing container that aligns by that horizontal guide, because explicit values project up through stacks. 3. **There is nothing to align against.** Guides are relative; a lone child shifted by its own guide has no sibling to move away from, though its new value can still reach a parent. ## Side effects to expect - The stack's bounds must enclose the shifted child, so the stack can grow and its other children can appear to move. Apple's documentation warns that changing the alignment of one view may affect surrounding views. - The override does not change the view's own size, unlike `padding(.leading, 16)`, which makes the view wider. Pick padding for a plain indent on a view without a background, and the guide when the view's frame must stay the same. - The closure is `@Sendable` in the SDK, so compute from `ViewDimensions` and constants rather than capturing mutable state. ## Guide, padding or offset | Tool | Moves the view in layout | Changes the view's size | Typical use | |---|---|---|---| | `alignmentGuide(_:computeValue:)` | yes, relative to siblings on the active guide | no | indent or outdent one child, line up a special view | | `padding(_:_:)` | yes, by adding space | yes | plain indentation where size does not matter | | `offset(x:y:)` | no; Apple describes it as shifting the rendered content from its position | no | visual nudges and animation, without moving neighbours | `offset` is the tool people reach for first and the one that surprises them: the view draws somewhere else while its siblings still lay out around its original place. A guide changes the layout itself, so the stack and its neighbours account for the move. ## A worked example ```swift VStack(alignment: .leading, spacing: 4) { Text("Latte") Text("Oat milk") .font(.caption) .alignmentGuide(.leading) { d in d[.leading] - 16 } Text("Croissant") } ``` "Oat milk" reports its leading guide 16 points before its real leading edge, so the stack places it 16 points to the trailing side of the other lines.
- Why not indent the line with padding(.leading, 16) instead?Padding works and is simpler, but it makes the view 16 points wider, which shows if the view has a background, border or tap area. `alignmentGuide` moves the view without changing its size. Use padding for a plain indent and the guide when the view's own frame must stay as it is.
- Which guide overrides does an HStack use when placing its children?Only vertical ones, because its alignment is a `VerticalAlignment`: an override of `.top`, `.center`, `.bottom`, `.firstTextBaseline` or `.lastTextBaseline` matters when it matches the stack's alignment. A horizontal override on an `HStack` child is not used by that `HStack`, though an enclosing container aligning by that guide can still pick it up.
- alignmentGuide works like offset and moves the view by the returned number.
- Overriding .leading on a child of a default VStack still indents it.
- Returning d[.leading] - 10 moves the view 10 points toward the leading edge.
- alignmentGuide changes the size of the view it modifies.
- An HStack uses a .leading override to place its children.