Search code examples
swiftuiswiftui-navigationlinkswiftui-navigationviewswiftui-environment

How to dismiss sheet from within NavigationLink


In the following example, I have a view that shows a sheet of ViewOne. ViewOne has a NavigationLink to ViewTwo.

How can I dismiss the sheet from ViewTwo?

Using presentationMode.wrappedValue.dismiss() navigates back to ViewOne.

struct ContentView: View {

    @State private var isShowingSheet = false

    var body: some View {
        Button("Show sheet", action: {
            isShowingSheet.toggle()
        })
        .sheet(isPresented: $isShowingSheet, content: {
            ViewOne()
        })
    }
}

struct ViewOne: View {

    var body: some View {
        NavigationView {
            NavigationLink("Go to ViewTwo", destination: ViewTwo())
                .isDetailLink(false)
        }
    }
}

struct ViewTwo: View {

    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        Button("Dismiss sheet here") {
            presentationMode.wrappedValue.dismiss()
        }
    }
}

Solution

  • This may depend some on platform -- in a NavigationView on macOS, for example, your existing code works.

    Explicitly passing a Binding to the original sheet state should work:

    struct ContentView: View {
    
        @State private var isShowingSheet = false
    
        var body: some View {
            Button("Show sheet", action: {
                isShowingSheet.toggle()
            })
            .sheet(isPresented: $isShowingSheet, content: {
                ViewOne(showParentSheet: $isShowingSheet)
            })
        }
    }
    
    struct ViewOne: View {
        @Binding var showParentSheet : Bool
        
        var body: some View {
            NavigationView {
                NavigationLink("Go to ViewTwo", destination: ViewTwo(showParentSheet: $showParentSheet))
                    //.isDetailLink(false)
            }
        }
    }
    
    struct ViewTwo: View {
        @Binding var showParentSheet : Bool
        @Environment(\.presentationMode) var presentationMode
    
        var body: some View {
            Button("Dismiss sheet here") {
                showParentSheet = false
            }
        }
    }