skip to content

A SwiftUI design system's primary button, card and labelled-value row serve twenty screens; how do you decide between style protocols, wrapper views and view modifiers for each?

level: principalimportance: should knowfreq 35%

answer

  1. does SwiftUI already have the control
  2. who owns the structure
  3. cross-cutting treatments
  4. set once, override locally
  5. deployment target limits the toolbox

basics

~20 s

Style the standard control when SwiftUI has one (ButtonStyle, LabeledContentStyle), so screens keep its semantics; build a generic container view when the component owns structure and slots; use a view modifier for treatments any view can take.

solid answer

~40 s

I start from what SwiftUI already provides. If a standard view exists, such as `Button` or `LabeledContent`, the component is a style: screens keep writing `Button` with its role and standard interaction, and one `.buttonStyle(.primary)` near the root sets the look, with local overrides where needed. If the component owns structure, like a card with a header and body, it is a generic container view with `@ViewBuilder` slots, because no style protocol describes a card. Cross-cutting treatments any view can take, such as a card surface or tap-to-reveal, become `ViewModifier` types behind `View` extensions. The trade-off is control versus consistency: wrapper views around system controls give total control but hide semantics and invite parameter sprawl. The deployment target also matters: the iOS 18 subview APIs are only available when the floor allows them.

go deeper

for a junior

Recall the three tools: style protocols for standard controls, container views with @ViewBuilder slots, and view modifiers for treatments any view can take.

for a middle

Explain how each tool reaches call sites: style modifiers propagate down, containers wrap caller content, modifiers wrap a single view.

for a senior

Spot the costs in review: wrapper views that hide Button, parameter sprawl, modifiers standing in for missing structure, and iOS 18 APIs in an iOS 17 app.

for a principal

Map each component to a mechanism with explicit criteria, weigh control against consistency and change cost, and set the governance that keeps twenty screens aligned.

## The decision a lead owns A shared component layer outlives any one screen. Once twenty screens use the primary button, changing its *shape* (not its colour) is expensive, so the lead's real decision is which **SwiftUI mechanism** each component is built on. There are three, and they are not interchangeable: - **Style protocols** (`ButtonStyle`, `LabeledContentStyle`, `LabelStyle`, `ToggleStyle`, ...): restyle a standard view everywhere below the modifier. - **Container views**: generic views with `@ViewBuilder` slots that own layout and decoration. - **View modifiers**: `ViewModifier` types or `View` extensions that add a treatment to any view. There is no single right answer for every component; the criteria below are where the judgment lies. ## Criteria, component by component | Question | Points to | |---|---| | Does SwiftUI already have this control or view? | A style protocol | | Does the component own structure or several slots? | A container view | | Can any view take this treatment? | A view modifier | | Does it need its own interaction rules? | `PrimitiveButtonStyle`, reviewed carefully | Applied to the fintech example: 1. **Primary button: a `ButtonStyle`.** Call sites keep `Button("Transfer", role: ..., action: ...)`, so the role, standard interaction and system behaviours stay. A static member lets screens write `.buttonStyle(.primary)`, and a modifier applied high in the hierarchy sets the default for a whole flow. 2. **Labelled-value row: a `LabeledContentStyle`.** Screens write `LabeledContent("Available", value: amount)`, and `.labeledContentStyle(.amountRow)` on a container restyles every row. 3. **Card: a container view.** No style protocol describes a card, and the card owns padding, background and slot layout. A generic `Card<Header, Content>` with an optional header keeps callers to content only. 4. **Card surface, reveal-on-tap: modifiers.** These apply to anything and may hold `@State`, which a `ViewModifier` type supports. ## Trade-offs worth arguing **Wrapper views around system controls.** A `PrimaryButton(title:action:)` view feels simple and gives total control. The costs: - Call sites stop being `Button`, so roles, the `.buttonStyle` overrides and other button-aware behaviour have to be re-plumbed through new parameters. - Every new need (an icon, a loading state, a destructive variant) adds a parameter, and the initializer sprawls. - Screens that need something slightly different bypass the wrapper and hand-roll a button, which is the drift the design system exists to stop. **Styles everywhere.** A style rearranges and decorates the pieces its configuration provides; it cannot give a control new slots; trying to force a card into a `GroupBoxStyle` ties the card to `GroupBox`'s semantics, which may not match. **Modifiers everywhere.** A modifier cannot enforce a structure; ten modifiers in a fixed order at every call site are a wrapper view in disguise. ## Moving an existing app Most teams inherit wrapper views rather than designing from scratch. A low-risk path: 1. Write the `ButtonStyle` or `LabeledContentStyle` that reproduces the wrapper's look. 2. Re-implement the wrapper's body as a standard `Button` with that style, so old call sites keep working. 3. Migrate screens to `Button` plus `.buttonStyle(.primary)` one flow at a time, then delete the wrapper. Each step ships on its own, and a screen never mixes two implementations of the same component. ## Rollout and governance - **Set defaults once.** Apply the app's styles near the root, and treat local overrides as deliberate exceptions visible in review. - **Name intents, not looks.** `.primary`, `.amountRow` survive a redesign; `.blueRounded` does not. - **Keep values separate.** Colours and spacing belong to the design tokens behind the components, which is design work rather than SwiftUI mechanics. - **Respect the deployment floor.** Containers built on `ForEach(subviews:)` or `Group(subviews:)` need iOS 18. An app supporting iOS 17 either gates them with `#available` or keeps a data-driven container API until the floor moves. - **Show every component in previews**, so a change to a shared style is reviewed visually before it reaches twenty screens. ## What a strong answer sounds like A strong candidate does not pick one mechanism for everything. They explain which mechanism each component maps to, why, what it costs to change later, and how the team keeps screens from drifting.

  • A designer wants the primary button to show a spinner while a transfer is pending; does that change the mechanism?
    Not necessarily. A `ButtonStyle` can read an environment value the screen sets, or the button's label can contain the spinner. If loading state needs to be passed at every call site, the team may wrap the button, but it should then keep `Button` inside and forward `role`, so the style and semantics survive.
  • How do you stop screens from drifting away from the shared components?
    Make the right thing the easy thing: static members such as `.primary`, defaults applied once near the root, a preview catalogue that shows every component, and code review that treats local overrides and hand-rolled controls as exceptions needing a reason. Drift usually starts where a shared component lacked a parameter, so track those requests.
  • When is a PrimitiveButtonStyle justified in a design system?
    Only when the interaction itself must differ, such as hold-to-confirm on an irreversible payment. The style then owns activation and must call `trigger()`, so the team takes on rebuilding behaviour the standard interaction provided. That cost is why it should be rare and reviewed, not the default for every button.

  • Every component should be a wrapper view so the team has full control.
  • A ButtonStyle cannot read anything beyond its configuration.
  • One mechanism, applied uniformly, is the mark of a good design system.
  • Group(subviews:) is safe in an iOS 17 app without an availability check.
  • Applying styles near the root prevents any screen from overriding them.

Assumes the iOS 27 SDK with an iOS 17 deployment floor; the subview-based container APIs it weighs need iOS 18.