For a team's shared SwiftUI component library, when would you standardise on a custom Layout instead of nested stacks or GeometryReader?
answer
- measure siblings before placing
- built-ins first
- GeometryReader takes offered space
- a few vetted layouts
- the modifier is the contract
basics
~20 sAdopt a custom Layout when an arrangement must measure siblings against offered space, like wrapping chips or equal-width buttons; keep stacks for what they express, and keep GeometryReader out of sizing, since it takes flexible space.
solid answer
~50 sMy rule for a shared library is built-in containers first, because everyone can read them and Apple's docs frame `Layout` as the tool for behaviour the built-ins can't achieve. A custom `Layout` earns its place when the arrangement must measure children before placing them, such as wrapping filter chips, an equal-width button row or a radial menu, and it pays back because it reports a real size to its parent and works with `AnyLayout`. `GeometryReader` is the wrong tool for sizing content: it returns a flexible size, so the component stops hugging its content and you still do the arithmetic by hand. I'd keep the set small and owned, expose per-child options through `LayoutValueKey` modifiers, document what each layout does with a `nil` width, and require previews at large Dynamic Type sizes before a new one merges.
go deeper
Recall that stacks cover most arrangements, and a custom Layout is for ones like wrapping chips that stacks cannot express.
Explain how a Layout reports a real size from measured children, while GeometryReader takes a flexible size and needs manual positioning.
Show the cases where a Layout pays off, its contract for nil widths and oversized children, and the previews that catch breakage.
Set the library policy: built-ins first, a small owned set of layouts, options as modifiers, accessibility previews, and caching only after profiling.
## The decision a lead owns On a single screen, choosing between an `HStack`, a `GeometryReader` and a custom `Layout` is a local call. In a **shared component library** used by many screens and many engineers it becomes policy: which arrangements get a reusable custom container, who maintains them, and what is discouraged. There is no single right answer, but there is a defensible default, and the reasoning behind it is what matters. ## Start from what each tool does | Tool | How it sizes itself | Strength | Cost | |---|---|---|---| | Nested `HStack`, `VStack`, `ZStack` | from its children, with alignment and spacing | every SwiftUI engineer reads it | cannot wrap, equalise widths or place on a curve | | `GeometryReader` | returns a flexible size to its parent | reads the space it was given | stops hugging content; positions are computed by hand | | Custom `Layout` (iOS 16) | `sizeThatFits` reports a real size from measured children | measures before placing; works with `AnyLayout` | a protocol to learn, review and test | | `ViewThatFits` or `AnyLayout` | picks or switches between existing arrangements | adaptive layout with little code | only chooses among arrangements you already have | Apple's own framing sets the default: use the built-in containers, and create a custom `Layout` "if you need behavior that you can't achieve with the built-in layout tools". ## When a custom Layout earns its place A custom `Layout` is the right standard when these hold together: - **The arrangement needs measurement before placement**: chips that wrap, buttons that all take the widest one's width, items on a circle, a masonry grid that fills the shortest column. - **It recurs.** Several screens want it, so one vetted implementation beats a handful of hand-tuned copies. - **Built-ins plus `ViewThatFits` or `AnyLayout` cannot express it** without guesswork such as pre-splitting arrays into rows. When they do, a `Layout` gives what the alternatives cannot. It takes part honestly in the parent's size proposal, so the component hugs its content. It can be swapped with the built-in layouts through `AnyLayout` without losing child state. And per-child options travel on the children through `LayoutValueKey`. ## Why GeometryReader is the usual anti-pattern `GeometryReader` is tempting because it hands you a width. Apple's documentation says it "returns a flexible preferred size to its parent layout", so a component built on it expands to fill whatever it is offered instead of fitting its content, and the engineer still positions every child by hand. In a library this spreads: each consumer has to wrap the component in a frame to stop it growing. Keep `GeometryReader` for reading geometry, which belongs with geometry and preferences, not for building containers. ## A policy a team can follow 1. **Built-ins first.** "Could this be stacks?" is a welcome review comment on any new layout. 2. **A small, named set of layouts**, such as a flow layout and an equal-width row, each with an owner and previews. 3. **A written contract** for each: what it does with a `nil` width, how it treats a child wider than the row, and its default spacing. 4. **Options as modifiers** backed by private `LayoutValueKey` types, so call sites read like SwiftUI. 5. **Accessibility and locale previews** at the largest Dynamic Type sizes and in right-to-left, because hand-written arithmetic is where those break first. 6. **No cache until a profile asks for one**; Apple notes most simple layouts gain little from it. ## Where it stops A custom `Layout` is not the tool for everything that feels custom: - a `Layout` receives a proxy for every subview at once and is not lazy, so long scrolling content stays in lazy containers; - decorating each child of a container, such as numbering or separators, is a job for the reusable-container techniques covered with reusable components; - moving a view between unrelated containers is a hero-transition problem, not a layout one. Knowing these edges is part of the answer: the lead's job is to keep the custom layer small enough that every engineer on the team still reads it as ordinary SwiftUI.
- How would you test a shared custom Layout?With previews of the states that stress it: no children, one child, a child wider than the row, the largest Dynamic Type sizes and right-to-left, plus snapshot tests if the team runs them. The row-breaking arithmetic can also be pulled into a pure function of sizes and widths and unit-tested without rendering anything.
- When would you accept GeometryReader in a library component?When the component is meant to fill its offered space and genuinely needs to read it, such as a full-bleed header, and its consumers expect that flexible size. Even then, the lighter geometry-reading modifiers covered with geometry and preferences are often enough, and the component should say in its documentation that it expands.
- Every repeated arrangement should become a custom Layout for consistency.
- GeometryReader is the standard way to size library components to their content.
- A custom Layout is lazy, so it suits long scrolling lists.
- Custom layouts cannot be combined with AnyLayout or the built-in stacks.
- Each custom Layout should ship with a cache by default.