Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 46 ยท Views and Layout

How SwiftUI Layout Works

Understand SwiftUI layout as a three-step negotiation: the parent proposes a size, the child chooses its own, the parent places it. Covers frame, fixedSize, GeometryReader and ViewThatFits.

Most layout confusion in SwiftUI comes from one wrong picture: that a parent tells its children how big to be. It does not. SwiftUI layout is a short negotiation, and once you can recite its three steps, behaviours that looked random (a frame that does not stretch an image, text that wraps early, a view that spills out of its parent) become predictable.

This lesson explains the negotiation, shows how frame, fixedSize, GeometryReader and ViewThatFits fit into it, and gives you a tiny Swift model you can run. The actual SwiftUI code is read-only, because iOS cannot run on this site. Build on the stack lesson and the modifier order lesson: modifiers are just more views in this negotiation.

The three steps

Layout is a conversation

  1. Propose
The parent offers the child a size.
  1. Choose
The child picks its own size. It may ignore the offer.
  1. Place
The parent positions the child in its own space.

The child always has the final word on its size.

1 / 2
  1. The parent proposes a size to a child. The proposal can be a concrete width and height, or nil for either, which means "tell me your ideal size".
  2. The child chooses its own size, using the proposal as a hint. It then reports that size back.
  3. The parent places the child inside its own bounds, usually centered unless an alignment says otherwise.

The root of the tree is the screen's safe area: the system proposes that whole area to your top-level view. That view proposes something to its child, and so on down the tree. Sizes flow down as proposals and back up as choices.

sequenceDiagram
    participant P as Parent
    participant C as Child
    P->>C: "Proposal: 200 x 400"
    C-->>P: "Chosen size: 112 x 20"
    Note over P: Parent places the child (centered by default)

The proposal travels down, the chosen size comes back up, and the parent decides where the child goes.

Who takes what

Different views answer a proposal differently. A Text chooses the size its content needs, but never more than the proposed width: it wraps onto more lines first, then truncates. An Image that is not marked resizable() chooses its own pixel size. A Color or a shape such as Rectangle accepts whatever it is offered. A Spacer takes as much as the stack gives it.

That explains a classic surprise:

Swift
Image("photo")
    .frame(width: 100, height: 100)   // does NOT scale the photo

The frame is a new parent that proposes 100 by 100, but a non-resizable image ignores the proposal and keeps its natural size. The fix is to make the child willing to accept the proposal:

Swift
Image("photo")
    .resizable()
    .scaledToFit()
    .frame(width: 100, height: 100)

A model of the negotiation

The program below models the protocol with a few small types. Each "view" receives a proposal, prints what it was offered and what it chose, and returns its size. Text wrapping here is simplified (it wraps by characters, not words), so treat the numbers as an illustration.

Swift Propose, choose, place

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

Read the trace for case B. The padding view was offered 100, passed only 68 to the text, and the text answered with a taller, narrower block. In case D, fixedSize ignored the 60 it was offered and proposed nil, so the text reported its ideal one-line width.

frame, fixedSize and the ideal size

frame(width:height:) with fixed numbers inserts an invisible parent that proposes those exact numbers to its child and reports exactly those numbers upward. It is a new layout container, not a property of the child. The child may still choose differently, and the frame will still be as big as you asked.

frame(minWidth:maxWidth:...) is the flexible version: it clamps the proposal into a range, so .frame(maxWidth: .infinity) means "accept whatever width is offered" and is the usual way to make a view fill its row.

fixedSize() does the opposite of accepting a proposal. It proposes nil to its child, which asks for the ideal size. For text the ideal size is the whole string on one line, so Text("A long label").fixedSize() refuses to wrap or truncate. You can limit it to one axis: .fixedSize(horizontal: false, vertical: true) lets the text wrap to the offered width but always grow tall enough to show every line, which cures clipped multi-line text.

GeometryReader and ViewThatFits

GeometryReader is the way to read the proposed size. It always accepts the whole proposal (it is greedy) and passes a GeometryProxy to its closure, whose size is what it was offered.

Swift
GeometryReader { proxy in
    Text("Half the width")
        .frame(width: proxy.size.width / 2)
}

Because it takes all available space, a GeometryReader can unexpectedly expand a layout. Reach for it only when you really need a measurement, for example sizing something as a fraction of its container.

ViewThatFits takes several candidate views and shows the first one that fits the offered space at its ideal size. It suits adaptive designs: a wide row of icons plus labels, falling back to icons only when narrow.

Swift
ViewThatFits {
    HStack { Image(systemName: "star"); Text("Add to favorites") }
    Image(systemName: "star")
}

For layouts that the built-in stacks cannot express, the Layout protocol lets you write a container yourself. You implement sizeThatFits(proposal:subviews:cache:), which answers the parent's proposal, and placeSubviews(in:proposal:subviews:cache:), which does step 3. Those two methods are exactly the "choose" and "place" steps of this lesson. You will rarely need it, but knowing it exists explains how the built-in stacks work.

The second model shows two of these ideas: a parent that centers a child larger than itself, and a ViewThatFits-style choice.

Swift Overflow and adaptive choice

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

The first two lines explain why an oversized child "spills out" evenly on both sides: the parent never shrinks its child, it just places it. The last line shows the fallback in this model; check how your own ViewThatFits behaves in a preview if the last candidate also does not fit.

When a layout misbehaves, say the three steps aloud for the offending view: what was it proposed, what size did it choose, and where did its parent place it? One of those answers is usually not what you assumed.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the correct order of SwiftUI layout?

    Layout is a negotiation: propose, choose, place. The child always chooses its own size.
  2. 2. Why does Image("photo").frame(width: 100, height: 100) not scale the photo?

    The frame proposes 100 by 100, but a non-resizable image chooses its own size. Adding resizable() makes it accept the proposal.
  3. 3. What does fixedSize() propose to its child?

    A nil proposal means "tell me your ideal size". For text that is the full string on one line, which is why fixedSize stops wrapping.
  4. 4. What is a notable behaviour of GeometryReader?

    GeometryReader takes the whole proposal and reports it as proxy.size, so it can expand a layout unexpectedly.

Summary

  • Layout is a negotiation: the parent proposes, the child chooses its own size, the parent places it.
  • A nil proposal means "your ideal size"; fixedSize() sends it on purpose.
  • frame(width:height:) adds a parent with a fixed size; it does not resize the child, which may still choose differently.
  • GeometryReader is greedy and exposes the offered size; use it sparingly.
  • ViewThatFits picks the first candidate that fits, and the Layout protocol lets you write the choose and place steps yourself.

Next, put these ideas to work on repeated content in List, ForEach and Identifiable.