Contents

actionSheet(isPresented:content:)

Presents an action sheet when a given condition is true.

Declaration

nonisolated func actionSheet(isPresented: Binding<Bool>, content: () -> ActionSheet) -> some View

Parameters

  • isPresented:

    A binding to a Boolean value that determines whether to present the action sheet that you create in the modifier’s content closure. When the user presses or taps the sheet’s default action button the system sets this value to false dismissing the sheet.

  • content:

    A closure returning the ActionSheet to present.

Discussion

In the example below, a button conditionally presents an action sheet depending upon the value of a bound Boolean variable. When the Boolean value is set to true, the system displays an action sheet with both destructive and default actions:

struct ConfirmEraseItems: View {
    @State private var isShowingSheet = false
    var body: some View {
        Button("Show Action Sheet", action: {
            isShowingSheet = true
        })
        .actionSheet(isPresented: $isShowingSheet) {
            ActionSheet(
                title: Text("Permanently erase the items in the Trash?"),
                message: Text("You can't undo this action."),
                buttons:[
                    .destructive(Text("Empty Trash"),
                                 action: emptyTrashAction),
                    .cancel()
                ]
            )}
    }

    func emptyTrashAction() {
        // Handle empty trash action.
    }
}

[Image]

See Also

View presentation modifiers