Spacing & Alignment in Stacks
Tune spacing and alignment between stacked views.
Spacing & Alignment in Stacks is a free SwiftUI Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Fine-Tuning Your Stacks
You can stack views in three directions, but real layouts need control over the gaps and edges between them. Let's tune that. 🎛️
The spacing Argument
Every stack accepts a spacing value that sets the gap between its children. A bigger number spreads views farther apart.
VStack(spacing: 20) {
Text("Top")
Text("Bottom")
}Zero Spacing
Pass spacing: 0 to remove the gap entirely, so children sit flush against each other. This is handy for tight, edge-to-edge designs.
Default Spacing
If you skip the argument, SwiftUI picks a sensible default gap that matches Apple's design guidelines. You only override it when you need to.
Aligning a VStack
A VStack's alignment argument controls horizontal placement. Use .leading to left-align children instead of centering them.
VStack(alignment: .leading) {
Text("Name")
Text("Email")
}Leading, Center, Trailing
VStack alignment offers .leading, .center, and .trailing. They decide whether children hug the left, the middle, or the right edge.
Aligning an HStack
An HStack's alignment is vertical instead. Use .top, .center, or .bottom to line children up along their tops, middles, or bottoms.
HStack(alignment: .top) {
Text("Tall")
Text("Short")
}Spacing and Alignment Together
You can set both arguments at once. Alignment comes first, then spacing, giving you full control over the layout in one line.
VStack(alignment: .leading, spacing: 8) {
Text("A")
Text("B")
}Meet Spacer
A Spacer is a flexible view that expands to push its neighbors apart, soaking up all the leftover space in a stack.
HStack {
Text("Left")
Spacer()
Text("Right")
}Pushing Views to Edges
Drop a Spacer between two views to send them to opposite ends, or before one view to shove it fully to the trailing side.
Baseline Alignment for Text
For mixed font sizes, HStack offers .firstTextBaseline so labels of different sizes share a clean reading line.
Quick Check
Let's check what each stack's alignment controls.
Recap: Spacing & Alignment
Well done! Use spacing to set gaps, alignment to position children, and Spacer to push views to the edges of any stack. 🚀
Frequently asked questions
Is the “Spacing & Alignment in Stacks” lesson free?
Yes — the full text of “Spacing & Alignment in Stacks” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Spacing & Alignment in Stacks”?
Tune spacing and alignment between stacked views. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Spacing & Alignment in Stacks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Stacking Views Vertically
- Rows with HStack
- Layering with ZStack
- Spacing & Alignment in Stacks