SwiftUI has unexpected behaviors with .sheet
that I haven't been able to work out. Here is a simple Master > Detail View that uses the same modal. One in a NavigationView
the other in .navigationBarItems
:
struct MasterView: View {
@State var showModal: Bool = false
var modal: some View {
ModalView(showModal: $showModal)
}
var body: some View {
NavigationView {
VStack {
Button("Can be dismissed") { self.showModal = true }.sheet(isPresented: $showModal) { self.modal }
NavigationLink(destination: DetailView()) { Text("Can't be dismissed") }
}
}
}
}
struct DetailView: View {
@State var showModal: Bool = false
var modal: some View {
ModalView(showModal: $showModal)
}
var body: some View {
Text("Detail View")
.navigationBarItems(trailing: Button("Dismisss?") { self.showModal = true }.sheet(isPresented: $showModal) { self.modal })
}
}
struct ModalView: View {
@Binding var showModal: Bool
var body: some View {
VStack {
Text("Modal View")
Button("Dismiss") { self.showModal = false }
}
}
}
The issue is that, though ModalView
can be dismissed from the Button
in MasterView
it cannot be dismissed with the .navigationBarItems
in DetailView
Anyone know how to dismiss a .sheet
invoked from .navigationBarItems
?
EDIT: Interestingly if you switch
Text("Detail View")
with
Button("Can Dismiss") { self.showModal = true }.sheet(isPresented: $showModal) { self.modal }
You get very unexpected results, causing the .navigationBarItems
to be disabled unless the modal was dismissed via a drag gesture
EDIT 2: This behavior has now been filed as a bug with id: FB6891155