How it works
ActionSheet is a value type that describes a set of related choices SwiftUI presents in a sheet anchored to the bottom of the screen (on iPhone) or in a popover (on iPad and Mac). Reach for it at a single decision point — typically when a user is about to commit to or discard something — to offer a short menu of alternatives that includes a clearly marked destructive option and a way to back out, all without navigating away from the current context. You build the value declaratively from a title, an optional message, and an array of buttons, then hand it to a presentation modifier that brings it on screen when a bound condition becomes true. This keeps *what* the choices are separate from *when* they appear, so presentation stays driven by your view's state.
Construct the sheet with ActionSheet(title:message:buttons:)
The initializer takes the content of the sheet as plain values rather than a layout closure: a
titlethat states the decision, an optionalmessagethat adds context, and abuttonsarray describing each choice. In the example the sheet asksText("Save Changes?")with the supporting lineText("You have unsaved edits."), and SwiftUI handles the styling and placement.Describe each choice with an ActionSheet.Button
The
buttonsarray is built from theActionSheet.Buttonfactory methods, each pairing a label with an optional action and a role that controls its emphasis. Use.default(Text("Save"))for ordinary choices,.destructive(Text("Discard"))to render a choice in the system's red warning style, and.cancel()to provide the standard dismissal that the platform positions apart and styles for you.Present it with the actionSheet(isPresented:content:) modifier
An
ActionSheetnever appears on its own; you attach it to a view with the.actionSheet(isPresented:)modifier, whose trailing closure returns the configured sheet. Here the modifier is applied to theText("Tap to choose an action")view, so the sheet anchors to that part of the hierarchy when presentation is requested.Drive presentation from a Boolean binding
The
isPresentedparameter takes aBinding<Bool>that controls visibility: SwiftUI shows the sheet while the value istrueand sets it back tofalseautomatically when the user taps a button or dismisses. The example wires it to$showing, backed by the@State private var showingdeclared on the view, so the sheet's lifetime is owned by state rather than imperative show/hide calls.
.default(Text("Save As Copy")) into the buttons array and watch it stack above the cancel button in the order you list it.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct ActionSheetDemo: View {
@State private var showing = true
var body: some View {
Text("Tap to choose an action")
.padding()
.actionSheet(isPresented: $showing) {
ActionSheet(
title: Text("Save Changes?"),
message: Text("You have unsaved edits."),
buttons: [
.default(Text("Save")),
.destructive(Text("Discard")),
.cancel()
]
)
}
}
}