DismissAction
An action that dismisses a presentation.
Declaration
type DismissAction = { (): Promise<void>; };@MainActor @preconcurrency struct DismissActionOverview
Use the EnvironmentValues.dismiss environment value to get the instance
of this structure for a given Environment. Then call the instance
to perform the dismissal. You call the instance directly because
it defines a DismissAction.callAsFunction()
method that Swift calls when you call the instance.
You can use this action to:
- Dismiss a modal presentation, like a sheet or a popover.
- Pop the current view from a
NavigationStack.
On apps targeting iOS 18 and aligned releases, you also use the dismiss
action to pop the implicit stack of a collapsed NavigationSplitView, or
clear the equivalent state in an expanded split view.
The specific behavior of the action depends on where you call it from.
For example, you can create a button that calls the DismissAction
inside a view that acts as a sheet:
private struct SheetContents: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
Button("Done") {
dismiss()
}
}
}When you present the SheetContents view, someone can dismiss
the sheet by tapping or clicking the sheet's button:
private struct DetailView: View {
@State private var isSheetPresented = false
var body: some View {
Button("Show Sheet") {
isSheetPresented = true
}
.sheet(isPresented: $isSheetPresented) {
SheetContents()
}
}
}Be sure that you define the action in the appropriate environment.
For example, don't reorganize the DetailView in the example above
so that it creates the dismiss property and calls it from the
View.sheet(item:onDismiss:content:) view modifier's content
closure:
private struct DetailView: View {
@State private var isSheetPresented = false
@Environment(\.dismiss) private var dismiss // Applies to DetailView.
var body: some View {
Button("Show Sheet") {
isSheetPresented = true
}
.sheet(isPresented: $isSheetPresented) {
Button("Done") {
dismiss() // Fails to dismiss the sheet.
}
}
}
}If you do this, the sheet fails to dismiss because the action applies
to the environment where you declared it, which is that of the detail
view, rather than the sheet. In fact, in macOS and iPadOS, if the
DetailView is the root view of a window, the dismiss action closes
the window instead.
The dismiss action has no effect on a view that isn't currently
presented. If you need to query whether SwiftUI is currently presenting
a view, read the EnvironmentValues.isPresented environment value.
From Apple’s SwiftUI documentation. Code samples there are Swift.
See also
More in Presentation.
- AutomaticPresentationSizingThe default presentation sizing, appropriate for the platform.
- DismissBehaviorProgrammatic window dismissal behaviors.
- DismissWindowActionAn action that dismisses a window associated to a particular scene.
- FileDialogBrowserOptionsThe way that file dialogs present the file system.
- FittedPresentationSizingThe size of the presentation is dictated by the ideal size of the content.
- FormPresentationSizingThe size is appropriate for forms and slightly less wide than.page
- MenuActionDismissBehaviorThe set of menu dismissal behavior options.
- MenuOrderThe order in which a menu presents its content.
- PagePresentationSizingThe size is roughly the size of a page of paper, appropriate for informational or compositional content.
- PopoverAttachmentAnchorAn attachment anchor for a popover.
- PresentationAdaptationStrategies for adapting a presentation to a different size class.
- PresentationBackgroundInteractionThe kinds of interaction available to views behind a presentation.