In SwiftUI, how does a custom ButtonStyle differ from a PrimitiveButtonStyle, and how does a ButtonStyle draw the pressed state?
answer
- appearance versus interaction
- makeBody(configuration:)
- isPressed lives on one configuration
- trigger() lives on the other
- buttonStyle(_:) reaches every button below
basics
~20 sButtonStyle replaces a button's appearance but keeps the platform's standard interaction, and its configuration's isPressed flag lets the style draw the pressed look. PrimitiveButtonStyle also takes over interaction: the style decides when to call configuration.trigger().
solid answer
~40 sBoth protocols have one requirement, `makeBody(configuration:)`, and both are applied with `buttonStyle(_:)`, which styles every button in the subtree. A `ButtonStyle` keeps SwiftUI's standard button interaction and gives me `ButtonStyleConfiguration`: the `label`, the `role` and `isPressed`. SwiftUI does not add a pressed highlight to a custom style, so I draw one from `isPressed`, for example with a lower opacity or a slight scale. A `PrimitiveButtonStyle` gets `PrimitiveButtonStyleConfiguration` instead: `label`, `role` and a `trigger()` method, with no `isPressed`, because the style owns the interaction and calls `trigger()` from its own gesture. I use it only for unusual interactions such as hold-to-confirm; to restyle while keeping standard behaviour I can wrap `Button(configuration)`.
code
swift · 28 linesimport SwiftUI
struct PrimaryButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
PrimaryButtonBody(configuration: configuration)
}
}
private struct PrimaryButtonBody: View {
let configuration: ButtonStyleConfiguration
@Environment(\.isEnabled) private var isEnabled
var body: some View {
configuration.label
.font(.headline)
.frame(maxWidth: .infinity, minHeight: 48)
.foregroundStyle(.white)
.background(
configuration.role == .destructive ? Color.red : Color.accentColor,
in: .rect(cornerRadius: 12)
)
.opacity(isEnabled ? (configuration.isPressed ? 0.7 : 1) : 0.4)
}
}
extension ButtonStyle where Self == PrimaryButtonStyle {
static var primary: PrimaryButtonStyle { PrimaryButtonStyle() }
}go deeper
Recall that ButtonStyle has one method, makeBody(configuration:), and that buttonStyle(_:) on a container styles every button inside it.
Explain the split: ButtonStyle keeps standard interaction and exposes isPressed, while PrimitiveButtonStyle owns interaction and exposes trigger(). Show how a style draws the pressed and destructive looks.
Judge when custom interaction is justified, rebuild it deliberately when it is, and catch styles that drop pressed or disabled feedback before they ship to many screens.
Treat button styles as the design system's contract for actions: one named style per intent, applied high in the hierarchy, with custom interaction reserved for rare, reviewed cases.
## Two protocols, one modifier SwiftUI separates **what a button looks like** from **how it is activated**, and gives you a style protocol for each level of control: - **`ButtonStyle`**: Apple's documentation says it "applies standard interaction behavior and a custom appearance to all buttons within a view hierarchy". You draw the button; SwiftUI keeps deciding when a tap counts. - **`PrimitiveButtonStyle`**: it "applies custom interaction behavior and a custom appearance". You draw the button and you decide when the action fires. Both have a single requirement, **`makeBody(configuration:)`**, and both are applied with **`buttonStyle(_:)`**, which has an overload for each protocol. The built-in styles you already use (`.bordered`, `.borderedProminent`, `.borderless`, `.plain`) are declared in the SDK as `PrimitiveButtonStyle` conformers, so `.buttonStyle(.bordered)` uses one overload of `buttonStyle(_:)` and a custom `ButtonStyle` uses the other. ## What each configuration gives you | Property | `ButtonStyleConfiguration` | `PrimitiveButtonStyleConfiguration` | |---|---|---| | `label` (the button's content as a view) | Yes | Yes | | `role` (for example `.destructive`, iOS 15 and later) | Yes | Yes | | `isPressed` | Yes | No | | `trigger()` | No | Yes | The difference in the table follows from who owns interaction. With `ButtonStyle`, SwiftUI tracks the touch, so it can tell you whether the button is currently pressed. With `PrimitiveButtonStyle`, the configuration reports no touch state at all; you attach your own gesture and call **`trigger()`** when your rule is met. ## Drawing the pressed state A custom `ButtonStyle` replaces the system appearance entirely, including the system's pressed feedback. If the style ignores `isPressed`, the button gives no visual response to touch. The usual fix is small: 1. Read `configuration.isPressed` in `makeBody(configuration:)`. 2. Change something visible: opacity, a darker fill, or a slight `scaleEffect`. 3. Optionally animate the change with `animation(_:value:)` keyed on `isPressed` (animation itself is its own topic). Two more inputs are worth reading in a design-system style: - **`role`**: Apple's documentation notes custom styles can read the button's role and adjust appearance, so a destructive "Close account" button can turn red without a second style. - **`isEnabled`**: an environment value whose default is `true`. `disabled(_:)` still blocks the action, but a distinct disabled look appears only if the style draws it. Reading `@Environment(\.isEnabled)` inside a small view returned from `makeBody(configuration:)` keeps that lookup in an ordinary view. ## Applying a style across a hierarchy `buttonStyle(_:)` applied to a container affects **all the buttons inside it**. The documentation adds that it also reaches controls that acquire a button style through composition, such as a `Menu` given `.menuStyle(.button)` or a `Toggle` given `.toggleStyle(.button)`. A common design-system touch is a static member so call sites read like the built-ins: ```swift extension ButtonStyle where Self == PrimaryButtonStyle { static var primary: PrimaryButtonStyle { PrimaryButtonStyle() } } // Button("Transfer", action: transfer).buttonStyle(.primary) ``` ## When PrimitiveButtonStyle is the right tool Reach for it only when the **interaction** must change: a hold-to-confirm payment button, a button that fires on press-down rather than release. Because the style owns activation, any behaviour of the standard interaction that you still need has to be rebuilt deliberately. If all you want is a different look around the standard behaviour, either use `ButtonStyle`, or, inside a `PrimitiveButtonStyle`, wrap the standard button with **`Button(configuration)`** and decorate it. ## Reviewing a shared button style Because one style will render every primary action in the app, a reviewer checks more than the happy path: - **Pressed feedback** comes from `isPressed`, not from the system. - **Disabled feedback** comes from `isEnabled`; without it, a disabled transfer button looks tappable. - **Roles**: a destructive or cancel role should still read as such once the style replaces the system look. - **Text size**: a fixed `frame(height:)` can clip large Dynamic Type sizes, so a minimum height is safer than an exact one (accessibility has its own lesson; the point here is that the style owns the frame). - **Label content**: callers may pass text, an image or both through `label`, so the style should not assume a single `Text`. ## Common mistakes - Expecting a custom `ButtonStyle` to show a pressed highlight without reading `isPressed`. - Looking for `isPressed` on `PrimitiveButtonStyleConfiguration`; it is not there. - Writing a `PrimitiveButtonStyle` that draws its own view but never calls `trigger()`, so the action never runs. - Applying the style to every button individually instead of once on a container.
- How do you restyle buttons inside a PrimitiveButtonStyle without rebuilding the standard tap handling?Wrap `Button(configuration)`, the initializer that builds a standard button from a `PrimitiveButtonStyleConfiguration`, and decorate it with modifiers. The wrapped button keeps the platform's activation behaviour and calls the action itself, so you only add appearance. If appearance is all you need, though, a plain `ButtonStyle` is simpler.
- Does buttonStyle(_:) affect a Toggle or Menu in the same subtree?Only when that control renders as a button. Apple's documentation shows a `Menu` with `.menuStyle(.button)` and toggles with `.toggleStyle(.button)` picking up the `.bordered` button style applied to their container, because they acquire a button style through composition. A toggle shown as a switch is unaffected.
- How would a custom ButtonStyle adapt to a destructive button?Read `configuration.role`, which is available from iOS 15. When it equals `.destructive`, change the fill or text colour. That lets one design-system style serve both normal and destructive actions, and the role still carries its meaning to the rest of the system.
A ButtonStyle repaints a doorbell's cover while the building's wiring still decides when it rings; a PrimitiveButtonStyle rewires the bell, so you decide what press makes it ring, and nothing rings unless you connect it.
- A custom ButtonStyle can change when the button's action fires.
- SwiftUI adds a pressed highlight to a custom ButtonStyle automatically.
- PrimitiveButtonStyleConfiguration has isPressed, just like ButtonStyleConfiguration.
- buttonStyle(_:) has to be applied to each Button separately.
- A PrimitiveButtonStyle that draws its own view runs the action on tap without calling trigger().