In SwiftUI, how do you present a sheet from a Boolean state value, and how does the sheet's own view close it?
answer
- presentation is state you own
- a binding SwiftUI can write back
- sheet(isPresented:onDismiss:content:)
- dismiss read inside the sheet's view
basics
~20 sAttach sheet(isPresented:) to a view and bind it to an @State Bool; setting it to true presents the content closure. Inside the sheet's own view, read @Environment(.dismiss) and call dismiss(), or set the Bool back to false.
solid answer
~40 s`sheet(isPresented:onDismiss:content:)` takes a `Binding<Bool>`: the presenting view owns `@State private var isAddingContact = false`, a button sets it to `true`, and SwiftUI presents whatever the `content` closure returns. Because it holds a binding, SwiftUI writes `false` back when the person drags the sheet down, so the state stays in step with the screen. To close the sheet from inside, the sheet's own view declares `@Environment(\.dismiss) private var dismiss` and calls `dismiss()`; the presenter can also set the Bool to `false`. `onDismiss` is the closure SwiftUI runs when the sheet goes away, useful for follow-up work such as reloading a list. The classic slip is reading `dismiss` in the presenting view and calling it from the content closure: that action belongs to the presenter's environment, so the sheet stays up.
code
swift · 28 linesimport SwiftUI
struct ContactsScreen: View {
@State private var isAddingContact = false
var body: some View {
Button("Add Contact") {
isAddingContact = true
}
.sheet(isPresented: $isAddingContact, onDismiss: {
print("Add-contact sheet closed")
}) {
NewContactView()
}
}
}
struct NewContactView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
VStack(spacing: 16) {
Text("New Contact")
Button("Done") { dismiss() }
}
.padding()
}
}go deeper
Recall the shape: an @State Bool, the sheet(isPresented:) modifier with a content closure, and dismiss read from the environment inside the sheet's own view.
Explain why the binding matters: SwiftUI writes false back on a swipe, and the dismiss action targets the environment it was read in, which is why it must live inside the sheet's view.
Show you keep presentation state honest in real screens: one source of truth per presentation, onDismiss for follow-up work, and a dedicated content view so dismiss always reaches the sheet.
Frame presentation as declarative state: the reviewer's question is who owns each presentation flag, and whether the screen's modal state can be read and reset from one place.
## Presentation is a value you own In SwiftUI you never call a "present" method on a view. A **modal presentation** (a sheet, a full-screen cover, a popover, an alert) is declared with a **view modifier**, and the modifier watches a piece of state that you own. Apple's documentation on modal presentations puts it this way: you provide a `Binding` to the condition, SwiftUI detects when the condition changes and makes the presentation for you, and because it holds a binding it can reset the underlying value when the person dismisses the presentation. Three consequences are worth saying out loud in an interview: - **The source of truth is your state**, usually a private `@State` Bool on the presenting view. - **SwiftUI writes back through the binding**, so after a swipe-down the Bool is `false` again without any code of yours. - **The modifier, not the button, owns the presentation.** The button only flips a value; the `sheet` modifier decides what appears. ## Presenting with sheet(isPresented:onDismiss:content:) The modifier has been in SwiftUI since iOS 13. Its declaration is `sheet(isPresented: Binding<Bool>, onDismiss: (() -> Void)? = nil, content: @escaping () -> Content)`. A contacts screen that opens an "add contact" form looks like this: ```swift struct ContactsScreen: View { @State private var isAddingContact = false var body: some View { Button("Add Contact") { isAddingContact = true } .sheet(isPresented: $isAddingContact) { NewContactView() } } } ``` Two habits keep this clean: - Keep the `content` closure a short call into a **dedicated view type** such as `NewContactView`. That view can then declare its own state and read its own environment values. - Use `onDismiss` for **follow-up work** after the sheet goes away (reloading a list, clearing a selection), not for building content. ## Three ways the sheet closes 1. **The person drags it down.** This is the interactive dismissal; SwiftUI sets the bound Bool to `false`. 2. **Code inside the sheet calls the `dismiss` action** from the environment. 3. **The presenter sets the Bool to `false`** itself, for example after a timer or a network response. | Way to close | Where the code lives | What it needs | |---|---|---| | Swipe down | nowhere, the system handles it | nothing | | `dismiss()` | a view inside the sheet | `@Environment(\.dismiss)` read in that view | | `isAddingContact = false` | the presenting view | access to the state or a binding to it | Whichever path runs, the end state is the same: the Bool is `false` and the sheet is gone. ## The dismiss action belongs to where you read it `@Environment(\.dismiss)` hands you a `DismissAction` (iOS 15 and later). You call it like a function because the type defines `callAsFunction()`. What it does depends on **where you read it**: inside a view that a sheet presents, it dismisses that sheet; inside a view pushed onto a navigation stack, it pops that view (covered with stack-based routing). Apple's documentation calls out the mistake directly. If the presenting view declares `dismiss` and calls it from the sheet's `content` closure, the sheet **fails to dismiss**, because the action applies to the environment where it was declared, which is the presenter's. In macOS and iPadOS, if that presenter is the root view of a window, the action can close the window instead. The fix is structural: move the button into a separate view type that is the sheet's content, and read `dismiss` there. Two related environment values: - `isPresented` (iOS 15) tells a view whether it is currently presented. The documentation notes that `dismiss` has no effect on a view that is not presented. - `presentationMode` is the iOS 13 predecessor. The SDK marks it for deprecation with the message "Use isPresented or dismiss", so new code should not reach for it. ## What to know next - **Landscape iPhone.** Apple's sheet documentation says that in vertically compact environments, such as an iPhone in landscape, a sheet adapts to appear as a full-screen cover. `presentationCompactAdaptation(_:)` (iOS 16.4) overrides that. - **Data for the sheet.** When the sheet needs to know *which* contact to show, prefer `sheet(item:onDismiss:content:)`, which hands the item to the closure instead of reading a second piece of state. - **Height.** A plain sheet rests at full height; `presentationDetents(_:)` (iOS 16) lets it rest lower. A junior answer that names the binding, shows `@State` driving it, and puts `dismiss` inside the sheet's own view covers what an interviewer is listening for.
- Why does calling dismiss() declared in the presenting view fail to close the sheet?A `DismissAction` acts on the environment where it is read. Declared in the presenter, it targets the presenter, not the sheet, so Apple's documentation says the sheet fails to dismiss; on iPadOS or macOS it may even close the presenter's window if that view is the window root. Read `dismiss` in a separate view type that is the sheet's content, or set the presentation Bool to `false`.
- How can a view find out whether it is currently being shown as a presentation?Read `@Environment(\.isPresented)`, available from iOS 15. It is `true` while the view is presented. The documentation also notes that calling `dismiss` on a view that is not presented has no effect, so `isPresented` is the way to decide, for example, whether to show a Close button at all.
- What does a sheet look like on an iPhone in landscape, and can you change it?Apple's sheet documentation says that in a vertically compact environment, such as iPhone landscape, a sheet adapts to appear as a full-screen cover. From iOS 16.4 you can apply `presentationCompactAdaptation(_:)` inside the sheet's content to request a different adaptation, for example `.none` to keep the sheet appearance.
- You present a sheet by building the view and calling a present method on it.
- After a swipe-down the Bool stays true until your code resets it.
- Reading dismiss in the presenter and calling it inside the sheet closure closes the sheet.
- onDismiss is where you build the content the sheet shows.
- New code should use presentationMode.wrappedValue.dismiss() to close a sheet.