TechnologiesSwiftUIPresentation and Dialogs

ActionSheet struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A representation of an action sheet presentation.

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.

  1. 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 title that states the decision, an optional message that adds context, and a buttons array describing each choice. In the example the sheet asks Text("Save Changes?") with the supporting line Text("You have unsaved edits."), and SwiftUI handles the styling and placement.

  2. Describe each choice with an ActionSheet.Button

    The buttons array is built from the ActionSheet.Button factory 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.

  3. Present it with the actionSheet(isPresented:content:) modifier

    An ActionSheet never 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 the Text("Tap to choose an action") view, so the sheet anchors to that part of the hierarchy when presentation is requested.

  4. Drive presentation from a Boolean binding

    The isPresented parameter takes a Binding<Bool> that controls visibility: SwiftUI shows the sheet while the value is true and sets it back to false automatically when the user taps a button or dismisses. The example wires it to $showing, backed by the @State private var showing declared on the view, so the sheet's lifetime is owned by state rather than imperative show/hide calls.

Try it — Insert a second ordinary choice such as .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.

ActionSheet.swift
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()
                    ]
                )
            }
    }
}
Live preview
Tap to choose an action Save Changes? You have unsaved edits. Save Discard Cancel
Tap to choose an action Save Changes? You have unsaved edits. Save Discard Cancel
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →