In SwiftUI, how do you stop a contact-edit sheet from being swiped away with unsaved changes while Cancel and Save still close it?
answer
- only the gesture is blocked
- conditional on having changes
- draft compared with original
- interactiveDismissDisabled(_:) since iOS 15
basics
~10 sApply interactiveDismissDisabled(draft != original) inside the sheet so the swipe is blocked only while there are unsaved changes. Programmatic dismissal still works, so Save and Cancel call dismiss(), and Cancel can confirm discarding first.
solid answer
~50 s`interactiveDismissDisabled(_:)` (iOS 15) prevents the **non-programmatic** dismissal of the sheet or popover that contains the view: the drag-down on a sheet, the tap outside on a popover. It has no effect on programmatic dismissal, so `dismiss()` and resetting the presentation binding keep working. Give the editor an `original` and a `@State` `draft`, and pass the condition `draft != original`, so an untouched form can still be swiped away. Cancel checks the same condition: with no changes it calls `dismiss()`; with changes it shows a `confirmationDialog` whose destructive "Discard Changes" button calls `dismiss()`. Save writes the draft and then dismisses; if the save fails, keep the sheet up and show an alert. The modifier can go on any view in the sheet's hierarchy, and a visible drag indicator is optional when the sheet cannot be swiped away.
code
swift · 29 linesstruct Contact: Identifiable, Equatable { let id: String; var name: String }
struct ContactEditor: View {
let original: Contact
let onSave: (Contact) -> Void
@State private var draft: Contact
@State private var isConfirmingDiscard = false
@Environment(\.dismiss) private var dismiss
init(contact: Contact, onSave: @escaping (Contact) -> Void) {
original = contact
self.onSave = onSave
draft = contact
}
var body: some View {
Form {
TextField("Name", text: $draft.name)
Button("Save") { onSave(draft); dismiss() }
Button("Cancel") {
if draft == original { dismiss() } else { isConfirmingDiscard = true }
}
}
.confirmationDialog("Discard your changes?", isPresented: $isConfirmingDiscard) {
Button("Discard Changes", role: .destructive) { dismiss() }
}
.interactiveDismissDisabled(draft != original)
}
}go deeper
Recall that interactiveDismissDisabled(_:) blocks swiping a sheet away, and that buttons calling dismiss() still close it.
Explain interactive versus programmatic dismissal, why the condition should be draft != original, and that the modifier can sit on any view in the sheet.
Show the full guard: a draft and an original, Cancel confirming a discard, Save that stays open on failure, and equality limited to the fields the form edits.
Decide a team-wide rule for edit forms, so every sheet that holds user input protects it the same way and never blocks an untouched form.
## Interactive versus programmatic dismissal A presentation can close in two ways: - **Interactive dismissal**: a built-in gesture. Apple's documentation names them: dragging a sheet down, and clicking or tapping outside a popover. - **Programmatic dismissal**: your code closes it, by setting the presentation binding to `false` or `nil`, or by calling the `dismiss` environment action. An edit sheet needs the first blocked while the second keeps working. Without that, a person halfway through editing a contact flicks the sheet down and loses their changes without warning. ## interactiveDismissDisabled(_:) `interactiveDismissDisabled(_ isDisabled: Bool = true)` has been in SwiftUI since iOS 15. The documentation's key points: - It **conditionally** prevents interactive dismissal of presentations like popovers, sheets and inspectors; the parameter decides whether it applies. - You can apply it to **any view in the sheet's hierarchy**: the top-level content view, a `Form`, or a button inside it. - It has **no effect on programmatic dismissal**. Updating the binding or calling `dismiss` still closes the sheet. - On macOS, disabling interactive dismissal in a popover makes the popover nontransient. The modifier blocks the gesture silently, so the screen must make the way out obvious. ## Building the guard 1. **Keep an original and a draft.** The editor receives the contact, stores it as `original`, and edits a `@State` copy called `draft`. With `Contact: Equatable`, "has changes" is simply `draft != original`. 2. **Disable the swipe only when it matters.** `.interactiveDismissDisabled(draft != original)`. An untouched form still swipes away like any other sheet. 3. **Cancel asks before discarding.** With no changes, Cancel calls `dismiss()`. With changes, it sets a Bool that presents a `confirmationDialog` with a destructive "Discard Changes" button, which calls `dismiss()`. 4. **Save writes, then dismisses.** On success, hand the draft back and call `dismiss()`. On failure, do not dismiss: show an alert so the person can retry, with their edits intact. ```swift .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { if draft == original { dismiss() } else { isConfirmingDiscard = true } } } } .confirmationDialog("Discard your changes?", isPresented: $isConfirmingDiscard) { Button("Discard Changes", role: .destructive) { dismiss() } } .interactiveDismissDisabled(draft != original) ``` ## Why the condition matters | Choice | What people experience | |---|---| | No modifier | a stray swipe loses edits with no warning | | `interactiveDismissDisabled()` (always `true`) | even an untouched form cannot be swiped away, which feels broken | | `interactiveDismissDisabled(draft != original)` | swipe works until there is something to lose, then Cancel or Save is required | The always-on version is the common over-correction. It protects nothing extra and makes every presentation of the form, including a quick look, demand a button tap. Compute the condition from values, not from a flag set on the first keystroke: typing a character and deleting it leaves the form unchanged, and comparing `draft` with `original` gets that right while a "did edit" flag does not. ## Edge cases a senior engineer checks - **Where `dismiss` is read.** The editor must read `@Environment(\.dismiss)` in its own view, the sheet's content. Read in the presenting list, it would target the list and leave the sheet up. - **Which presentation is blocked.** The modifier affects the presentation that contains the view. A `confirmationDialog` or alert shown from inside the editor is a separate presentation with its own dismissal rules. - **Save failures.** Keep the sheet presented, keep the draft, and report the error with an alert; the swipe stays disabled because the draft still differs from the original. - **Equality that means something.** If `Contact` carries fields the form never edits, such as a last-synced timestamp, compare only the editable fields, or a background refresh can make an untouched form look dirty. - **A visible cue.** The documentation for `presentationDragIndicator(_:)` says you can show the grabber when a sheet cannot be dismissed interactively; whether to do so is a design call, and the Cancel button is the clearer signal. ## What belongs elsewhere Where the saved contact goes (a SwiftData model context, a repository, a network call) is persistence, not presentation. The toolbar placements `.cancellationAction` and `.confirmationAction` are covered with SwiftUI toolbars. The pattern here is only the presentation contract: block the gesture while there is something to lose, and keep every programmatic exit working.
- Does interactiveDismissDisabled stop dismiss() or a binding change from closing the sheet?No. Apple's documentation says the modifier has no effect on programmatic dismissal, which you invoke by updating the binding that controls the presentation or by calling the environment's `dismiss` action. That is what lets Save and a confirmed Cancel close a sheet whose swipe is blocked.
- Where in the sheet's view tree can the modifier go?Anywhere inside the presented hierarchy. The documentation says you can apply it to the sheet's top-level view or to any child, such as the `Form` or a button. What matters is that the view is inside the presentation you want to protect, not on the presenter outside it.
- What goes wrong if an edit sheet uses interactiveDismissDisabled() with no condition?The parameter defaults to `true`, so the swipe is blocked even when nothing has been edited. People opening the form just to look must find a button to leave, which reads as a broken sheet. Passing `draft != original` keeps the protection only while there are changes to lose.
- interactiveDismissDisabled(true) also stops dismiss() from closing the sheet.
- The modifier must sit on the presenting view next to the sheet modifier.
- Blocking the swipe permanently is the safest default for every edit form.
- A failed save should still dismiss the sheet and drop the draft.
- Reading dismiss in the presenting list lets the editor's Cancel close the sheet.