struct SheetView: View {
@Binding var showSheetView: Bool
@Environment(\.presentationMode) var mode
var body: some View {
NavigationView {
Text("Sheet View content")
.navigationBarTitle(Text("Sheet View"), displayMode: .inline)
.navigationBarItems(trailing: Button(action: {
print("Dismissing sheet view...")
// self.mode.wrappedValue.dismiss()
// self.showSheetView.toggle()
self.showSheetView = false
}) {
Text("Done").bold()
})
}
}
}
struct ContentView2: View {
@State var showSheetView = false
var body: some View {
NavigationView {
Text("first")
}.navigationBarTitle("asdf").navigationBarItems(trailing: Button(action: {
self.showSheetView.toggle()
}) {
Text("Show Sheet View")
}.sheet(isPresented: $showSheetView) {
SheetView(showSheetView: self.$showSheetView)
})
}
}
struct ContentView: View {
@State var showSheetView = false
var body: some View {
NavigationView {
NavigationLink(destination: ContentView2()) {
Text("ds")
}
}
}
}
SheetView在ContentView2中成功呈现。然后我单击“完成”按钮关闭并尝试再次显示它。它将永远失败。但是当我将启动按钮放在导航栏中时,该问题不存在。
答案 0 :(得分:1)
这是已知的缺陷,但不具有绑定功能-具有导航栏项目的布局...
这是解决方法
.navigationBarTitle("asdf").navigationBarItems(trailing: Button(action: {
self.showSheetView.toggle()
}) {
Text("Show Sheet View").padding() // << add padding !!
您可以在SwiftUI modal presentation works only once from navigationBarItems
中找到更多详细信息和替代解决方案