How it works
DismissBehavior is a type whose constant values describe whether an interaction within a presentation — such as a sheet or popover — is allowed to dismiss that presentation. SwiftUI uses these values to negotiate between system-provided gestures, like swipe-to-dismiss, and the requirements of your interface, where some flows must run to completion before the user moves on. Reach for it when a presentation needs to assert control over its own lifecycle rather than letting an incidental gesture tear it down. It turns dismissal from an implicit, gesture-driven event into a behavior you can declare.
Choose between the
enabledanddisabledconstantsDismissBehaviorexposes its values as static members —enabledpermits an interaction to dismiss the presentation, whiledisabledprevents it. You select the constant that matches the contract your flow needs: a casual sheet can stayenabled, but a step the user must acknowledge resolves todisabledso the presentation persists until you explicitly close it, as the demo'sDonebutton does withshowingSheet = false.Present the content the behavior governs
A
DismissBehavioronly has meaning relative to an active presentation, so it attaches to content surfaced by a modifier like.sheet(isPresented:). In the example the sheet is driven by$showingSheet, and everything inside that closure — including its dismissal rules — is the scope the behavior controls.Disable the interactive gesture with
interactiveDismissDisabledThe clearest expression of
disabledbehavior is theinteractiveDismissDisabled(true)modifier, which tells SwiftUI to suppress the swipe-to-dismiss gesture on the presentation. Passingtruehere is what makes the sheet refuse to be flicked away; passingfalsewould restore the system's defaultenabledbehavior.Provide an explicit exit path
When dismissal is
disabled, the system gesture no longer offers a way out, so your interface must supply one. The demo does this withButton("Done") { showingSheet = false }, flipping the@Stateflag that drivesisPresented— the deliberate dismissal that a locked presentation requires.
interactiveDismissDisabled(true) to interactiveDismissDisabled(false) and the sheet will once again accept the swipe-down gesture, restoring the default enabled behavior.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 DismissBehaviorDemo: View {
@State private var showingSheet = true
var body: some View {
VStack(spacing: 12) {
Text("Sheet stays put")
.font(.headline)
Text("Swipe-to-dismiss is disabled.")
.foregroundStyle(.secondary)
}
.padding()
.sheet(isPresented: $showingSheet) {
VStack(spacing: 16) {
Text("Important Step")
.font(.title2.bold())
Text("You can't swipe this away.")
.foregroundStyle(.secondary)
Button("Done") { showingSheet = false }
.buttonStyle(.borderedProminent)
}
.padding()
.presentationDetents([.medium])
.interactiveDismissDisabled(true)
}
}
}