Snippets
SwiftUI Alerts and Confirmation Dialogs
Use alerts for urgent decisions and confirmationDialog for action choices that can adapt across iPhone, iPad, and macOS.
4 min readUpdated 2026-06
import SwiftUI
struct DeleteProjectButton: View {
@State private var showingDialog = false
var body: some View {
Button("Delete Project", role: .destructive) {
showingDialog = true
}
.confirmationDialog("Delete this project?", isPresented: $showingDialog) {
Button("Delete", role: .destructive) { deleteProject() }
Button("Cancel", role: .cancel) {}
} message: {
Text("This removes local drafts and cannot be undone.")
}
}
}Implementation notes
Button roles are more than color; the system can use them for placement and emphasis.
Use alerts for a compact decision and confirmation dialogs for choosing among actions.
On compact devices, dialogs adapt differently from larger screens, so test both sizes.
Checklist
Keep the title short and action-oriented.
Use role: .destructive only for irreversible or risky actions.
Make sure there is always a safe cancellation path.
Related reference
SwiftUI Sheets, Popovers, and Detents
Present focused secondary work with sheets, popovers, and detents while keeping dismissal and size behavior explicit.
SwiftUI Toolbar and ControlGroup Actions
Place primary and secondary commands in toolbars with predictable placement, grouping, tinting, and keyboard-aware behavior.
SwiftUI disabled, redacted, and allowsHitTesting
Represent unavailable, loading, private, and pass-through states with the right interaction and visual modifiers.