Building a Flow Layout
Create a wrapping tag-style layout.
Building a Flow Layout 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.
What Is a Flow?
A flow layout places items left to right and wraps to a new line when they run out of room, like word-wrapped text. 🏷️
Perfect for Tags
This wrapping behavior is exactly what you want for tag chips, hashtags, or filter pills of varying widths.
Start with Layout
Build it by conforming to the Layout protocol so you control wrapping yourself, since no stack wraps automatically.
struct FlowLayout: Layout {
var spacing: CGFloat = 8
}Measure Each Chip
In sizeThatFits, ask every subview for its size, then simulate placing them to find the total height needed.
let sizes = subviews.map {
$0.sizeThatFits(.unspecified)
}Track a Cursor
Keep an x and a y cursor. Move x rightward for each chip and reset when the next chip would overflow.
var x: CGFloat = 0
var y: CGFloat = 0Detect Overflow
Before placing a chip, check if x plus its width exceeds the available width; if so, wrap to the next row.
if x + size.width > maxWidth {
x = 0
y += rowHeight + spacing
}Advance the Row
After placing a chip, move x forward by its width plus spacing and grow rowHeight to the tallest chip seen.
x += size.width + spacing
rowHeight = max(rowHeight, size.height)Place in the Second Pass
In placeSubviews, repeat the same wrapping math but call place at each computed point.
view.place(at: CGPoint(x: bounds.minX + x,
y: bounds.minY + y),
proposal: .unspecified)Return the Height
Your sizeThatFits should return the proposed width and the final y plus the last row height.
CGSize(width: maxWidth,
height: y + rowHeight)Drop It In
Now use it like any container: wrap your chips and the FlowLayout wraps them across as many lines as needed.
FlowLayout {
ForEach(tags, id: \.self) { tag in
TagChip(tag)
}
}Reusable Anywhere
Because it is a real Layout, your flow works with any views, adapts to width, and animates with the rest of SwiftUI. ✨
Quick Check
Recall what triggers a new row in a flow layout.
Recap
You built a wrapping FlowLayout with the Layout protocol, tracking a cursor to flow chips across rows automatically. 🎉
Frequently asked questions
Is the “Building a Flow Layout” lesson free?
Yes — the full text of “Building a Flow Layout” 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 “Building a Flow Layout”?
Create a wrapping tag-style layout. 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 “Building a Flow Layout” 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
- GeometryReader Essentials
- Custom Alignment Guides
- The Layout Protocol
- Building a Flow Layout