TechnologiesSwiftUIPresentation and Dialogs

PresentationMode struct

iOSmacOStvOSwatchOSvisionOS✓ renders

An indication whether a view is currently presented by another view.

How it works

PresentationMode is a structure that reports whether the view that owns it is currently presented, and gives that view a way to dismiss itself. SwiftUI publishes the active presentation through the environment, so a presented view can ask to be removed without holding a binding to whatever sheet, popover, or navigation push displayed it. Reach for it when a detail or modal view needs to close itself in response to a button tap or a completed task, rather than pushing that responsibility back up to the presenting view.

  1. Read the presentation from the environment

    PresentationMode is delivered through SwiftUI's environment under the presentationMode key, so you bring it in with the @Environment property wrapper rather than constructing it yourself. In the example, @Environment(\.presentationMode) var presentationMode binds the current presentation to a property the view can consult and act on.

  2. Unwrap the binding with wrappedValue

    The environment hands you a Binding<PresentationMode>, so you reach the structure itself through its wrappedValue. The example writes presentationMode.wrappedValue to get the live PresentationMode value before doing anything with it, since the wrapper, not the property, holds the current state.

  3. Inspect isPresented to know your state

    PresentationMode exposes an isPresented Boolean that is true while the view is on screen as part of a presentation. You can read it to drive conditional behavior — for instance, only running dismissal logic when the view is actually being presented — though the demo simply trusts that it is presented and offers a way out.

  4. Call dismiss() to remove the view

    The structure's dismiss() method asks SwiftUI to take the current view out of its presentation, closing the sheet or popping the navigation destination that showed it. The example wires this into a button so that tapping Button("Dismiss") runs presentationMode.wrappedValue.dismiss() and the screen closes itself.

Try it — Add a Text(presentationMode.wrappedValue.isPresented ? "On screen" : "Dismissed") above the button to watch isPresented flip as dismiss() takes effect.

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.

PresentationMode.swift
struct PresentationModeDemo: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        VStack(spacing: 16) {
            Text("Detail Screen")
                .font(.headline)
            Button("Dismiss") {
                presentationMode.wrappedValue.dismiss()
            }
            .buttonStyle(.borderedProminent)
        }
        .padding()
    }
}
Live preview
Detail Screen Dismiss
Detail Screen Dismiss
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →