【发布时间】:2019-08-10 07:40:37
【问题描述】:
我正在尝试关闭通过 SwiftUI 中的 .sheet 呈现的模态视图 - 由 Button 调用,该视图位于 NavigationViews navigationBarItems 内,如下所示:
struct ModalView : View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button(action: {
self.presentationMode.value.dismiss()
}, label: { Text("Save")})
}
}
struct ContentView : View {
@State var showModal: Bool = false
var body: some View {
NavigationView {
Text("test")
.navigationBarTitle(Text("Navigation Title Text"))
.navigationBarItems(trailing:
Button(action: {
self.showModal = true
}, label: { Text("Add") })
.sheet(isPresented: $showModal, content: { ModalView() })
)
}
}
}
点击“保存”按钮时,模式不会关闭,它只会保留在屏幕上。摆脱它的唯一方法是在模式上向下滑动。
打印self.presentationMode.value的值总是显示false所以好像还没有呈现出来。
仅当它从NavigationView 呈现时才会发生。把它拿出来就可以了。
我在这里遗漏了什么,还是这是一个测试版问题?
【问题讨论】:
-
当我在几个测试版之前尝试过类似的东西时,它出现了一次。我认为你不需要担心 showModal,它是由 sheet() 重置的
-
刚刚意识到我错过了问题中的关键句子 - 模态显示正常,但不会关闭!
标签: swiftui