DevPrep

All topics › SwiftUI

How does layout work in SwiftUI: size proposal, size choice, placement?

Middle PRO

The gist

Layout in SwiftUI is a three-step dialogue. The parent proposes a size to the child (ProposedViewSize: each axis is a number, nil for "whatever suits you", 0 for "minimum" or .infinity for "maximum"). The child chooses its own size taking the proposal into account, but it is not obliged to obey. The parent places the child in its coordinate space — it can no longer change the size the child chose.

Unlike Auto Layout there is no constraint system: every view is a function "proposal → size", layout runs top-down, and containers may query children several times with different proposals when needed.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store