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()
}
}
}