How does a custom SwiftUI LabeledContentStyle applied once to a container restyle every LabeledContent inside, and which style wins when nested?
answer
- makeBody(configuration:) again
- configuration holds the pieces, not values
- style modifiers propagate downward
- closest one wins
- a static member for the call site
basics
~20 sA custom style conforms to a style protocol such as LabeledContentStyle and builds the view in makeBody(configuration:) from the configuration's pieces. Its style modifier propagates down the hierarchy, so every matching view below uses it, and a style set closer to a view wins.
solid answer
~40 sI conform a struct to `LabeledContentStyle` and implement `makeBody(configuration:)`; the configuration gives me `label` and `content` as views, so the style decides arrangement and decoration without knowing what the caller put in them. I add a static member, `extension LabeledContentStyle where Self == AmountRowStyle`, so callers write `.labeledContentStyle(.amountRow)`. Apple's documentation says style modifiers typically propagate through container views, so applying the style once to a `VStack` or a whole screen restyles every `LabeledContent` inside. A style set closer to a view takes precedence; the toggleStyle documentation shows one toggle reverting to `.automatic` inside a container styled `.button`. `LabelStyle` works the same way with `title` and `icon`.
code
swift · 30 linesimport SwiftUI
struct AmountRowStyle: LabeledContentStyle {
func makeBody(configuration: Configuration) -> some View {
HStack(alignment: .firstTextBaseline) {
configuration.label
.foregroundStyle(.secondary)
Spacer()
configuration.content
.font(.body.monospacedDigit())
.fontWeight(.semibold)
}
}
}
extension LabeledContentStyle where Self == AmountRowStyle {
static var amountRow: AmountRowStyle { AmountRowStyle() }
}
struct AccountSummary: View {
var body: some View {
VStack(spacing: 8) {
LabeledContent("Balance", value: "$2,310.00")
LabeledContent("Pending", value: "-$120.00")
LabeledContent("Account", value: "Checking")
.labeledContentStyle(.automatic)
}
.labeledContentStyle(.amountRow)
}
}go deeper
Recall that style protocols share makeBody(configuration:) and that one style modifier on a container styles every matching view inside it.
Explain what a configuration carries (views, not data), how the static member enables .amountRow, and that the style set closer to a view wins.
Use propagation deliberately: set defaults high in the hierarchy, override locally with intent, and catch styles that silently stop applying because a view of a different type was used.
Decide which design-system components should be styles of standard views so screens keep system semantics and one modifier can change the look app-wide.
## Style protocols as a family SwiftUI ships a **style protocol** for many of its views: `ButtonStyle`, `ToggleStyle`, `LabelStyle`, `LabeledContentStyle` (iOS 16), `GroupBoxStyle`, `ProgressViewStyle` and others. Apple's *View styles* overview describes the shared pattern: SwiftUI picks an automatic style for the context, you can override it with a **style view modifier**, and any style protocol that defines `makeBody(configuration:)` lets you write a custom style and apply it "exactly like a built-in style". The protocols that support custom styles share one shape: - **`makeBody(configuration:)`**, a builder method that returns the styled view. - **A configuration type** carrying the view's pieces as opaque views, plus any state the control exposes. - **A matching modifier**: `labelStyle(_:)`, `labeledContentStyle(_:)`, `toggleStyle(_:)` and so on. ## What each configuration exposes | Protocol | Configuration pieces | Since | |---|---|---| | `LabelStyle` | `title`, `icon` | iOS 14 | | `LabeledContentStyle` | `label`, `content` | iOS 16 | | `ToggleStyle` | `label`, `isOn` | iOS 13 | | `ButtonStyle` | `label`, `isPressed`, plus `role` from iOS 15 | iOS 13 | The label and content pieces are **views, not data**. A `LabeledContentStyle` never sees the string "Available" or the amount; it receives a `label` view and a `content` view and decides where they go, which font they get and what sits between them. That is why one style can serve every labelled-value row in an app, whatever the caller passes in. Built-in label styles are `automatic`, `iconOnly`, `titleAndIcon` and `titleOnly`. A custom style can reuse the default rendering through initializers that take a configuration, such as `LabeledContent(configuration)` and `Label(configuration)`, and decorate it. ## How one modifier reaches many views Apple's documentation says style modifiers "typically propagate through container views, so you can wrap an entire view hierarchy in a style modifier to affect all the views of the given type". In practice: 1. Apply `.labeledContentStyle(.amountRow)` to a screen's root stack. 2. Every `LabeledContent` below it, however deep, renders with `AmountRowStyle` unless a closer style overrides it. 3. Views of other types ignore it; a label style does not touch buttons. This is what makes styles the backbone of a design system: a screen keeps using the standard view, and one modifier, possibly applied near the root of the app, sets the look. ## Which style wins when nested When a container sets one style and a view inside sets another, the **closer** style takes precedence for that view and its own descendants. Apple's `toggleStyle(_:)` documentation shows this directly: a stack sets `.toggleStyle(.button)`, and one toggle inside sets `.toggleStyle(.automatic)` to "override a container's style and revert to the default". ## Making a style configurable A design system usually needs a few variants of one style: a compact row for dense tables, a highlighted row for a total. Three options, in order of preference: 1. **Stored properties on the style**, exposed through static functions: `static func amountRow(emphasis: Bool) -> AmountRowStyle`. The variant is explicit at the call site and the type stays small. 2. **An environment value** the screen sets once, when a whole subtree should share the variant. Custom environment values belong to the environment lesson; here it is enough that a view the style returns can read one. 3. **A second style type**, when the variants differ so much that one `makeBody(configuration:)` would become a tangle of conditions. Whichever you choose, keep the number of variants small. Every new variant is a new way for screens to diverge. ## Pitfalls - **Expecting data in the configuration.** If the style needs extra input (a currency, a sign), pass it to the style's initializer or through the environment; the configuration carries only the pieces. - **Forgetting the static member.** Without `extension LabeledContentStyle where Self == AmountRowStyle`, callers must write `AmountRowStyle()` everywhere, and drift begins. - **Styling the wrong layer.** A label style restyles `Label` views; it has no effect on a `Text` that only looks like a label. - **Version assumptions.** `LabeledContentStyle` needs iOS 16, fine for an iOS 17 baseline but worth stating in a shared package.
- How can a custom style keep the system's default rendering and only add to it?Build the default view from the configuration and decorate it: `LabeledContent(configuration)` inside a `LabeledContentStyle`, or `Label(configuration)` inside a `LabelStyle`. Both initializers exist for this purpose, so the style can add a background or spacing while the platform still decides the core arrangement.
- A style needs the account's currency; where should that come from?Not from the configuration, which only carries the view's pieces. Either give the style a stored property set at the call site, as in `.labeledContentStyle(.amountRow(currencyCode: "EUR"))` via a static function, or read a value the screen places in the environment. The first is explicit; the second suits a value shared by a whole subtree.
A style modifier on a container works like a publication's house style: the editor sets it once and every article follows it, but an explicit instruction on one article wins for that article.
- A style modifier affects only the view it is written on.
- The outermost style always overrides styles set on children.
- A LabeledContentStyle receives the label's string and the value as data.
- A LabelStyle also restyles plain Text views that look like labels.
- A custom style must rebuild the default layout from scratch.