In SwiftUI, what does giving a Button a role such as .destructive or .cancel change, and what does it leave to you?
answer
- a role describes purpose
- styling chosen per context
- cancel replaces the default dismiss
- no confirmation comes for free
- iOS 26 adds two roles
basics
~20 sA ButtonRole tells SwiftUI what a button is for, so the system styles and places it per context: destructive draws red in menus, cancel takes a dialog's dismiss slot. It never adds confirmation or changes the action.
solid answer
~40 s`ButtonRole` is a description of purpose, set with `Button("Delete", role: .destructive, action: delete)`. SwiftUI uses it to style the button for its context — a destructive button in a context menu or a list row's swipe actions appears red — and dialogs may reorder buttons by role. In a `confirmationDialog`, a button with the `.cancel` role takes the place of the default dismiss action and goes to the standard position; in an `alert` with custom actions there is no default cancel, so you add one with that role. What a role never does: confirm, undo or skip the action. A destructive button in a `Form` runs its closure on the first tap, so a delete-everything row should present a dialog first. iOS 26 added `.confirm` and `.close`, plus `Button(role:action:)` with a system-supplied label.
code
swift · 22 linesimport SwiftUI
struct DownloadsSection: View {
@State private var confirmingDelete = false
let deleteAll: () -> Void
var body: some View {
Section {
Button("Delete All Downloads", role: .destructive) {
confirmingDelete = true
}
.confirmationDialog(
"Delete all downloaded episodes?",
isPresented: $confirmingDelete,
titleVisibility: .visible
) {
Button("Delete Downloads", role: .destructive, action: deleteAll)
Button("Keep Them", role: .cancel) {}
}
}
}
}go deeper
Recall the roles, how to pass one to Button, and that destructive draws red in menus while cancel takes the dialog's dismiss slot.
Explain that the role is metadata the system interprets per container, that dialogs may reorder by role, and that iOS 26 added confirm and close.
Show that you never rely on a role for safety: irreversible actions go through a confirmation, and newer roles sit behind availability checks.
Argue for a team rule that destructive actions always confirm or offer undo, using roles for meaning rather than for colouring.
## What a role is A **`ButtonRole`** (iOS 15) is a value that describes a button's purpose. You pass it to an initialiser such as `Button("Delete All Downloads", role: .destructive) { ... }` or `Button("Keep Them", role: .cancel) {}`. The role is metadata: it does not change the closure you supply, and a button with no role simply gets the default appearance for its context. On the iOS 27 SDK the type declares four values: | Role | Available from | Purpose | |---|---|---| | `.destructive` | iOS 15 | the action deletes or discards something | | `.cancel` | iOS 15 | the action backs out without changing anything | | `.confirm` | iOS 26 | the action accepts what the surrounding UI proposes | | `.close` | iOS 26 | the action closes the current presentation | iOS 26 also added `Button(role:action:)`, which takes no label at all: its label type is `DefaultButtonLabel`, so the system supplies the label for the role. An app that deploys to iOS 17 or 18 can only use the last two behind an `#available` check. ## What the system does with it Apple's documentation says the system uses the role to style the button appropriately in every context. In practice: - **Menus and context menus** draw a destructive button with a red foreground. - **Swipe actions** on a list row show a destructive button in red, as Apple's swipe-actions documentation notes. - In a **`confirmationDialog`**, a `.cancel` button takes the place of the dialog's default dismiss action, and the dialog puts it in its standard position even if you declared it first. You do not need to dismiss the dialog from the cancel button's closure: every action in a dialog dismisses it after running. - In an **`alert`** with custom actions, the documentation notes that no default cancel action is provided; add a `.cancel` button if the alert needs one. - Dialogs and alerts **may reorder** buttons by role and prominence, so do not rely on declaration order. - A custom button style can read the role from its configuration's `role` property — how to write such a style is a separate subject. ## What a role leaves to you The role is not a safety mechanism, and treating it as one is the common mistake. 1. **No confirmation.** `Button("Delete All Downloads", role: .destructive) { deleteAll() }` placed in a `Form` deletes on the first tap. To ask first, let the row set a flag and present a `confirmationDialog` whose own destructive button does the work. 2. **No undo.** Recovery is your model's job, not the button's. 3. **No guarantee of red.** The appearance is decided per context; a destructive button inside a plain `HStack` with a custom style may look like any other. Never use the role as a colouring trick for a non-destructive action — it also changes where the system may place the button. 4. **No change to enabled state.** A role does not disable anything; use `disabled(_:)` for that. ## A settings example A podcast app's downloads section shows the pattern: the row in the form is destructive in intent, and the irreversible work happens only inside the dialog. ```swift Button("Delete All Downloads", role: .destructive) { confirmingDelete = true } .confirmationDialog("Delete all downloaded episodes?", isPresented: $confirmingDelete, titleVisibility: .visible) { Button("Delete Downloads", role: .destructive, action: deleteAll) Button("Keep Them", role: .cancel) {} } ``` On iPhone the dialog appears as an action sheet with "Keep Them" in the cancel position. On iPad in a regular size class the documentation notes it appears as a popover dismissed by tapping outside it, which is one more reason the cancel button's closure should not hold required work. ## Checklist for a destructive action - The row or menu item that starts it carries `role: .destructive`. - It presents a dialog or another explicit confirmation before anything irreversible runs. - The dialog has a `.cancel` button with a clear label such as "Keep Them". - The destructive button inside the dialog names the consequence, such as "Delete Downloads", not just "OK". ## Interview angle A good answer separates **meaning** from **mechanics**: the role says what the button means, the system turns that into styling and placement per platform and container, and the app still owns confirmation, undo and enabling. Mentioning that `.confirm` and `.close` arrived in iOS 26, and so need an availability check in apps that support iOS 17 or 18, shows you read availability before reaching for a new API.
- Can you use the .close role in an app that still supports iOS 17?Only behind an availability check. `ButtonRole.close`, `.confirm` and `Button(role:action:)` are declared `@available(iOS 26.0, *)`, so the compiler rejects them when the deployment target is lower. Wrap the use in `if #available(iOS 26, *)` and fall back to a labelled button, or to `.cancel` where that meaning fits.
- Does a cancel button inside a confirmationDialog need to set the isPresented flag back to false?No. Every action in a confirmation dialog dismisses it after running, and a `.cancel` button replaces the default dismiss action. Its closure can be empty; put any clean-up there only if backing out must also reset other state.
- A destructive role makes SwiftUI ask the user to confirm
- The role only changes the text colour and nothing else
- Buttons in a dialog always appear in declaration order
- A cancel button must manually reset the dialog's binding
- The close and confirm roles work on iOS 17
Assumes the iOS 27 SDK; .destructive and .cancel date from iOS 15, while .confirm, .close and Button(role:action:) need iOS 26.