8.5 · The layout negotiation, stacks, GeometryReader, safe area and custom Layout

SwiftUI layout is a conversation: a parent proposes a size, the child chooses its own, and the parent places it. Every layout surprise is a misreading of that conversation.

Mental model. Parent proposes, child chooses, parent places. A child can take less, more or exactly what is offered, and a parent must work with the answer.

The negotiation

Proposals, stacks, frames and padding.

  1. How does SwiftUI’s layout negotiation work, and why do frame and padding order change the result? 8.5.1 · L3
    Layout is three steps repeated down the tree. The parent proposes a size to its child (possibly “unspecified”, “zero” or “infinity” to ask for the ideal…

Reading and bending layout

<code>GeometryReader</code>, safe areas and the <code>Layout</code> protocol.

  1. When do you use GeometryReader, and how do safe areas work in SwiftUI? 8.5.2 · L2
    GeometryReader gives its content a GeometryProxy with the size and safe-area insets of the space it was offered, and lets you read frames in named coordinate…
  2. How does the Layout protocol work, and when is a custom layout the right tool? 8.5.3 · L5
    A type conforming to Layout takes over the negotiation for its subviews. You implement two required methods. sizeThatFits(proposal:subviews:cache:) returns the…

More in SwiftUI