Skip to main content

How to make a view dismiss itself

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to make a view dismiss itself 관련

SwiftUI by Example

Back to Home

How to make a view dismiss itself | SwiftUI by Example

How to make a view dismiss itself

Updated for Xcode 15

When you show a SwiftUI view using a sheet, it’s common to want to dismiss that view when something happens – when the user taps on a button, for example. There are two ways of solving this in SwiftUI, and I’m going to show you both so you can decide which suits your needs.

The first option is to tell the view to dismiss itself using its presentation mode environment key. Any view can dismiss itself, regardless of how it was presented, using @Environment(\.dismiss), and calling that property as a function will cause the view to be dismissed.

For example, we could create a detail view that is able to dismiss itself using its presentation mode environment key, and present that from ContentView;

struct DismissingView1: View {
    @Environment(\.dismiss) var dismiss

    var body: some View {
        Button("Dismiss Me") {
            dismiss()
        }
    }
}

struct ContentView: View {
    @State private var showingDetail = false

    var body: some View {
        Button("Show Detail") {
            showingDetail = true
        }
        .sheet(isPresented: $showingDetail) {
            DismissingView1()
        }
    }
}

Download this as an Xcode projectopen in new window

The other option is to pass a binding into the view that was shown, so it can changing the binding’s value back to false. You still need to have some sort of state property in your presenting view, but now you pass that into the detail view as a binding.

Using this approach, the detail view settings its binding to false also updates the state in the original view, causing the detail view to dismiss – both the detail view and original view point to the same Boolean value, so changing one changes it in the other place too.

Here’s that in code:

struct DismissingView2: View {
    @Binding var isPresented: Bool

    var body: some View {
        Button("Dismiss Me") {
            isPresented = false
        }
    }
}

struct ContentView: View {
    @State private var showingDetail = false

    var body: some View {
        Button("Show Detail") {
            showingDetail = true
        }
        .sheet(isPresented: $showingDetail) {
            DismissingView2(isPresented: $showingDetail)
        }
    }
}

Download this as an Xcode projectopen in new window

Similar solutions…
How to dismiss the keyboard for a TextField | SwiftUI by Example

How to dismiss the keyboard for a TextField
How to dismiss the keyboard when the user scrolls | SwiftUI by Example

How to dismiss the keyboard when the user scrolls
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks
How to convert a SwiftUI view to an image | SwiftUI by Example

How to convert a SwiftUI view to an image
How to create and compose custom views | SwiftUI by Example

How to create and compose custom views

이찬희 (MarkiiimarK)
Never Stop Learning.