SwiftUI的.sheet
行为异常,我无法解决。这是一个使用相同模态的简单“主控>细节视图”。 NavigationView
中一个在.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 }
}
}
}
问题在于,尽管ModalView
可以从Button
的{{1}}中解雇,但不能与MasterView
的{{1}}一起被解雇
任何人都知道如何消除从.navigationBarItems
调用的DetailView
吗?
编辑:有趣的是,如果您切换
.sheet
使用
.navigationBarItems
您将获得非常意外的结果,除非禁用该模式,否则 Text("Detail View")
将被禁用
编辑2:现在,此行为已记录为ID为FB6891155的错误
答案 0 :(得分:1)
这绝对值得一个错误报告。最初,我认为使用@Environment(\.presentationMode)
可以解决此问题,但绝对不能改变行为。
我的发现是,如果只有1 sheet
,则可以使模态被忽略。即如果您从sheet
中删除了DetailView
通话。
struct MasterView: View {
@State var showModal: Bool = false
var modal: some View {
ModalView()
}
var body: some View {
NavigationView {
VStack {
Button("Can be dismissed") { self.showModal = true }.sheet(isPresented: $showModal) { self.modal }
NavigationLink(destination: DetailView(showModal: $showModal)) { Text("Can't be dismissed") }
}
}
}
}
struct DetailView: View {
@Binding var showModal: Bool
var body: some View {
Text("Detail View")
.navigationBarItems(trailing: Button("Dismisss?") { self.showModal = true })
}
}
struct ModalView: View {
@Environment(\.presentationMode) var showModal
var body: some View {
VStack {
Text("Modal View")
Button("Dismiss") {
self.showModal.value.dismiss()
}
}
}
}
虽然这确实使模态在程序上无法使用,但您最终还是遇到了您所提到的第二个错误,即禁用.navigationBarItems
直到返回SwiftUI button in navigation bar only works once