interactiveDismissDisabled
Conditionally prevents interactive dismissal of presentations like popovers, sheets, and inspectors.
Declaration
compose().interactiveDismissDisabled(isDisabled?: boolean)nonisolated func interactiveDismissDisabled(
_ isDisabled: Bool = true
) -> some ViewParameters
isDisabledbooleanA Boolean value that indicates whether to prevent nonprogrammatic dismissal of the containing view hierarchy when presented in a sheet or popover.
Discussion
Users can dismiss certain kinds of presentations using built-in
gestures. In particular, a user can dismiss a sheet by dragging it down,
or a popover by clicking or tapping outside of the presented view. Use
the interactiveDismissDisabled(_:) modifier to conditionally prevent
this kind of dismissal. You typically do this to prevent the user from
dismissing a presentation before providing needed data or completing
a required action.
For instance, suppose you have a view that displays a licensing agreement that the user must acknowledge before continuing:
struct TermsOfService: View {
@Binding var areTermsAccepted: Bool
@Environment(\.dismiss) private var dismiss
var body: some View {
Form {
Text("License Agreement")
.font(.title)
Text("Terms and conditions go here.")
Button("Accept") {
areTermsAccepted = true
dismiss()
}
}
}
}If you present this view in a sheet, the user can dismiss it by either
tapping the button --- which calls EnvironmentValues.dismiss
from its action closure --- or by dragging the sheet down. To
ensure that the user accepts the terms by tapping the button,
disable interactive dismissal, conditioned on the areTermsAccepted
property:
struct ContentView: View {
@State private var isSheetPresented = false
@State private var areTermsAccepted = false
var body: some View {
Button("Use Service") {
isSheetPresented = true
}
.sheet(isPresented: $isSheetPresented) {
TermsOfService(areTermsAccepted: $areTermsAccepted)
.interactiveDismissDisabled(!areTermsAccepted)
}
}
}You can apply the modifier to any view in the sheet's view hierarchy,
including to the sheet's top level view, as the example demonstrates,
or to any child view, like the Form or the Accept Button.
The modifier has no effect on programmatic dismissal, which you can
invoke by updating the Binding that controls the presentation, or
by calling the environment's EnvironmentValues.dismiss action.
On macOS, disabling interactive dismissal in a popover makes the popover
nontransient.
See also
More in Presentation.
- alertPresents an alert when a given condition is true, using a localized string resource for the title.
- confirmationDialogPresents a confirmation dialog when a given condition is true, using a localized string resource for the title.
- contextMenuAdds an item-based context menu to a view.
- dialogIconConfigures the icon used by dialogs within this view.
- dialogSuppressionToggleEnables user suppression of dialogs and alerts presented within self, with a custom suppression message on macOS. Unused on other platforms.
- dismissalConfirmationDialogPresents a confirmation dialog upon an attempt to dismiss the window for this view.
- documentBrowserContextMenuAdds to a DocumentLaunchView actions that accept a list of selected files as their parameter.
- fileDialogBrowserOptionsOn macOS, configures the fileExporter, fileImporter, or fileMover to provide a refined URL search experience: include or exclude hidden files, allow searching by tag, etc.
- fileDialogConfirmationLabelOn macOS, configures the fileExporter, fileImporter, or fileMover with a custom confirmation button label.
- fileDialogCustomizationIDOn macOS, configures the fileExporter, fileImporter, or fileMover to persist and restore the file dialog configuration.
- fileDialogDefaultDirectoryConfigures the fileExporter, fileImporter, or fileMover to open with the specified default directory.
- fileDialogImportsUnresolvedAliasesOn macOS, configures the fileExporter, fileImporter, or fileMover behavior when a user chooses an alias.