Contents

isPresented

A Boolean value that indicates whether the view associated with this environment is currently presented.

Declaration

var isPresented: Bool { get }

Discussion

You can read this value like any of the other EnvironmentValues by creating a property with the Environment property wrapper:

@Environment(\.isPresented) private var isPresented

Read the value inside a view if you need to know when SwiftUI presents that view. For example, you can take an action when SwiftUI presents a view by using the onChange(of:initial:_:) modifier:

.onChange(of: isPresented) { _, isPresented in
    if isPresented {
        // Do something when first presented.
    }
}

This behaves differently than onAppear(perform:), which SwiftUI can call more than once for a given presentation, like when you navigate back to a view that’s already in the navigation hierarchy.

To dismiss the currently presented view, use dismiss.

See Also

Dismissing a presentation