0Pricing
SwiftUI Academy · Lesson

The Layout Protocol

Implement sizeThatFits and placeSubviews.

The Layout Protocol is a free SwiftUI Academy lesson on CoddyKit — lesson 3 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.

Beyond Stacks

When stacks and grids are not enough, the Layout protocol lets you write your own container that positions children exactly how you want. 🧩

Two Required Methods

Conform to Layout by implementing sizeThatFits and placeSubviews, the two steps every custom layout needs.

struct MyLayout: Layout {
    func sizeThatFits(...) -> CGSize { ... }
    func placeSubviews(...) { ... }
}

Sizing Yourself

In sizeThatFits, you measure the children and return the total size your container wants to occupy.

func sizeThatFits(proposal: ProposedViewSize,
    subviews: Subviews,
    cache: inout ()) -> CGSize {
    CGSize(width: 200, height: 100)
}

The Proposal

The ProposedViewSize is the space the parent offers. You decide how to use or shrink within it.

let width = proposal.width ?? 0

The Subviews Proxy

The subviews argument is a collection of proxies. Ask each one for its preferred size before placing it.

for view in subviews {
    let size = view.sizeThatFits(.unspecified)
}

Placing Children

In placeSubviews, you loop and call place on each subview with an exact point and an anchor.

func placeSubviews(in bounds: CGRect,
    proposal: ProposedViewSize,
    subviews: Subviews,
    cache: inout ()) {
}

The place Call

Each place call sets the origin, the anchor point, and the size proposal for that one child.

view.place(at: point,
           anchor: .topLeading,
           proposal: .unspecified)

Walk a Cursor

A simple layout keeps an x cursor, placing each child then advancing by its width plus spacing.

var x = bounds.minX
for view in subviews {
    let s = view.sizeThatFits(.unspecified)
    view.place(at: CGPoint(x: x, y: bounds.minY),
               proposal: .unspecified)
    x += s.width
}

Using Your Layout

A Layout type is used just like a stack: wrap your views in it and they get arranged by your code.

MyLayout {
    Text("One")
    Text("Two")
}

Optional Cache

The cache parameter lets you store expensive measurements between passes; use Void when you do not need it.

It Is Value-Based

Because a Layout is a struct, SwiftUI can re-run it cheaply, keeping custom containers fast and predictable. ⚡

Quick Check

Recall the two methods every Layout must implement.

Recap

You can now build a custom container with the Layout protocol by measuring in sizeThatFits and positioning in placeSubviews. 🎉

Frequently asked questions

Is the “The Layout Protocol” lesson free?

Yes — the full text of “The Layout Protocol” 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 “The Layout Protocol”?

Implement sizeThatFits and placeSubviews. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Layout Protocol” 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

  1. GeometryReader Essentials
  2. Custom Alignment Guides
  3. The Layout Protocol
  4. Building a Flow Layout
← Back to SwiftUI Academy