Alerts, Sheets and Confirmation Dialogs
Alert
swift
struct AlertDemo: View {
@State private var showAlert = false
var body: some View {
Button("Delete account") { showAlert = true }
.alert("Delete account?", isPresented: $showAlert) {
Button("Delete", role: .destructive) { deleteAccount() }
Button("Cancel", role: .cancel) { }
} message: {
Text("This cannot be undone.")
}
}
func deleteAccount() { }
}
Confirmation Dialog (Action Sheet)
swift
@State private var showOptions = false
Button("Photo options") { showOptions = true }
.confirmationDialog("Choose", isPresented: $showOptions, titleVisibility: .visible) {
Button("Take photo") { }
Button("Choose from library") { }
Button("Remove", role: .destructive) { }
}
Sheet
swift
struct SheetDemo: View {
@State private var showSheet = false
var body: some View {
Button("Open settings") { showSheet = true }
.sheet(isPresented: $showSheet) {
SettingsView()
.presentationDetents([.medium, .large]) // half height or full
.presentationDragIndicator(.visible)
}
}
}
struct SettingsView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
NavigationStack {
Text("Settings go here")
.navigationTitle("Settings")
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") { dismiss() }
}
}
}
}
}
dismiss:
@Environment(\.dismiss) gives the sheet a way to close itself. Call dismiss() and SwiftUI handles the rest.
Sheet with an Item
swift
struct Book: Identifiable { let id = UUID(); let title: String }
@State private var selected: Book?
List(books) { book in
Button(book.title) { selected = book }
}
.sheet(item: $selected) { book in // opens when selected is not nil
Text("Details of \(book.title)")
}
Full Screen Cover and Popover
swift
.fullScreenCover(isPresented: $showOnboarding) {
OnboardingView()
}
.popover(isPresented: $showTip) {
Text("Tap to learn more").padding()
}
ProgressView
swift
ProgressView() // spinner
ProgressView("Loading...")
ProgressView(value: 0.6) // bar
ProgressView(value: downloaded, total: 100)
| Component | Use for |
|---|---|
| Alert | Short important message or yes/no question |
| Confirmation dialog | Choose between several actions |
| Sheet | A related task on a card |
| Full screen cover | Immersive flow like onboarding or camera |
Conclusion
Alerts interrupt, dialogs offer choices, sheets host sub-tasks and covers take the whole screen. Control them with a Boolean or an optional item.
Quick Summary: Use alert, confirmationDialog, sheet and fullScreenCover with isPresented or item bindings, and dismiss with @Environment(\.dismiss).

