skip to content

In SwiftUI, how does a view end up with its size, and what happens in the propose, choose and place exchange between parent and child?

level: middleimportance: must knowfreq 68%

answer

  1. a conversation, not an order
  2. the parent offers, the child answers
  3. zero, unspecified and infinity proposals
  4. a frame is just another parent

basics

~20 s

A SwiftUI parent proposes a size to each child, the child chooses its own size, and the parent positions it. A parent cannot impose a size; a modifier such as frame inserts a new parent that proposes differently.

solid answer

~50 s

Layout in SwiftUI is a recursive negotiation. Each parent proposes a size to its children, each child replies with the size it wants (smaller, equal to or larger than the offer), and the parent then places the child according to its alignment. Views answer differently: shapes and `Color` take whatever they are offered, `Text` takes what its string needs and wraps or truncates when offered less, and Apple's documentation describes controls like `Toggle` as having an ideal size that never varies. Since iOS 16 a proposal is modelled as `ProposedViewSize`, where each dimension is a number or `nil`; the zero proposal asks for a view's minimum, infinity for its maximum and the unspecified (`nil`) proposal for its ideal size. Modifiers such as `frame` and `fixedSize()` are wrapper views that change the proposal on the way down or the size reported on the way up.

go deeper

for a junior

Recall the three steps in order: the parent proposes a size, the child chooses its own, the parent places it. Be ready to say that a frame wraps a view rather than resizing it.

for a middle

Explain how different views answer a proposal: shapes take everything, Text takes what its string needs, controls keep a stable ideal size. Name the zero, infinity and unspecified proposals and what each asks for.

for a senior

Use the exchange to diagnose real screens: say which wrapper changed the proposal, which child overflowed it, and prove it with temporary borders rather than guessing at padding values.

for a principal

Frame the model as the reason a team avoids hard-coded sizes: views that answer proposals honestly adapt to Dynamic Type, split view and new devices, so reviews should question fixed frames around content.

## The three steps SwiftUI does not let a parent dictate a child's size. Instead, every layout pass is a short conversation that repeats recursively down the view hierarchy: 1. **Propose.** The parent offers the child a size. The root of the hierarchy is offered the space the window or screen gives it, minus the safe areas it respects by default. 2. **Choose.** The child looks at the offer and decides its own size. It may accept the offer, take less, or take more. Whatever it picks is final for that pass. 3. **Place.** The parent positions the child inside its own bounds, using its alignment (a `frame` centres its child by default; an `HStack` centres its children vertically by default). A container such as a stack usually derives its own size from the answers of its children, then reports that size upward to its own parent. Apple's documentation shows a `ZStack` that ends up the size of its largest child. ## What a proposal can say Since iOS 16, a proposal is represented by the `ProposedViewSize` struct, whose `width` and `height` are optional `CGFloat` values. Containers often probe a child with several special proposals to learn how flexible it is: | Proposal | Value | What the child is asked for | |---|---|---| | `.zero` | 0 by 0 | its **minimum** size | | `.infinity` | infinity in both dimensions | its **maximum** size | | `.unspecified` | `nil` in both dimensions | its **ideal** size | | a concrete size | for example 300 by `nil` | a fit for that width, ideal height | Apple notes that an `HStack` might measure the flexibility of its children's widths while holding the height fixed. You meet `ProposedViewSize` directly when you write a custom container with the `Layout` protocol; with built-in views it stays implicit, but the same exchange drives every screen. ## How different views answer Apple's guide to laying out a simple view groups built-in views into three families: - **Expanding views** such as `Color`, `LinearGradient` and `Circle` fill the space offered. A shape always occupies the space the layout system gives it. - **Content-sized views** such as `Text` and the container views have an ideal size that depends on their content. A `Text` offered less than its ideal width wraps, tightens, scales or truncates to fit. - **Fixed-ideal views** such as `Toggle` and `DatePicker` have, in Apple's words, an ideal size that never varies. This is why a `Circle` next to a short `Text` in an `HStack` balloons: the stack offers the circle its share of the width and the circle accepts every point of it. ## Modifiers are parents too Most layout modifiers are **wrapper views**. Apple's documentation says that when you add a frame modifier, SwiftUI wraps the affected view, effectively adding a new view to the hierarchy. That wrapper takes part in the same exchange: - `frame(width:height:)` proposes its fixed dimensions to the child and reports those same dimensions upward, whatever the child chose. - `frame(minWidth:idealWidth:maxWidth:...)` clamps the proposal it passes down and computes its own size from the proposal and the child's answer. - `fixedSize()` acts as a counter-proposal: it fixes the child at its ideal size, ignoring the offer from above. - `ignoresSafeArea(_:edges:)` makes the layout system propose the expanded size to the view. ## Seeing it on a product screen ```swift struct ProductHeader: View { let name: String var body: some View { HStack { Circle() .fill(Color.orange) .frame(width: 40, height: 40) // the frame proposes 40 x 40 Text(name) // takes what its string needs .font(.title2) } .border(Color.red) // temporary: shows the chosen size } } ``` Without the frame, the circle would accept most of the row. With it, the frame proposes 40 by 40, the circle accepts, and the stack has more width to offer the title. ## Common misreadings - **"The parent sets the child's size."** It only proposes; the child decides, and may overflow. - **"`frame` resizes the view."** It wraps the view in a new one with its own size; the original view still chooses for itself. - **"A `nil` dimension means zero."** It asks for the ideal size. - **"Every view fills its space."** Only expanding views do; content-sized and fixed-ideal views answer from their content. Adding a temporary `border(_:width:)` to each view in question is the quickest way to see which size each one actually chose.

  • Why does a Circle placed next to a short Text in an HStack grow so large?
    A shape always occupies the space it is offered. The `HStack` gives the `Text` what its string needs and offers the `Circle` its share of the rest, and the circle accepts all of it. Wrapping the circle in `frame(width: 40, height: 40)` changes the offer it receives, so the stack has more width left for the text.
  • What does a nil dimension in ProposedViewSize ask for, and where do you see one?
    A `nil` dimension is an unspecified proposal: it asks the view for its ideal size in that dimension. `fixedSize()` behaves as if the child had received that request, and a flexible frame's `idealWidth` or `idealHeight` replaces an unspecified dimension in the proposal it passes down. A custom `Layout` can send `.unspecified` explicitly.
  • How do you check which size each view actually chose while debugging a layout?
    Add a temporary `border(_:width:)` in a distinct colour to each view you suspect, or select the view in an Xcode preview, which outlines its bounds. Comparing the border of a `frame` wrapper with the border of the view inside it shows immediately whether the child accepted the proposal or overflowed it.

A venue offering a group a hall: the venue says how much room is available, the group decides how much of it they will actually use, and the venue then decides where in the building to seat them.

  • The parent sets the child's size and the child must fill it exactly.
  • frame(width:) resizes the original view rather than wrapping it in a new one.
  • Every SwiftUI view expands to fill whatever space it is offered.
  • A nil dimension in a size proposal means the view should be zero-sized.
  • Layout is purely top-down; children never report a size back to their parent.