在SwiftUI中,当您从导航栏项内的按钮显示模态时存在一个错误。 在下面的代码中,Button 1按预期工作,但Button 2仅能工作一次:
struct DetailView: View {
@Binding var isPresented: Bool
@Environment (\.presentationMode) var presentationMode
var body: some View {
NavigationView {
Text("OK")
.navigationBarTitle("Details")
.navigationBarItems(trailing: Button(action: {
self.isPresented = false
// or:
// self.presentationMode.wrappedValue.dismiss()
}) {
Text("Done").bold()
})
}
}
}
struct ContentView: View {
@State var showSheetView = false
var body: some View {
NavigationView {
Group {
Text("Master")
Button(action: { self.showSheetView.toggle() }) {
Text("Button 1")
}
}
.navigationBarTitle("Main")
.navigationBarItems(trailing: Button(action: {
self.showSheetView.toggle()
}) {
Text("Button 2").bold()
})
}.sheet(isPresented: $showSheetView) {
DetailView(isPresented: self.$showSheetView)
}
}
}
这个bug来自去年中期,仍存在于Xcode 11.3.1 + iOS 13.3模拟器和iOS 13.3.1 iPhone XS中。
有没有什么解决办法可以使按钮正常工作?
编辑:
- 似乎点击区域偏向下方,可能会点击按钮下方以显示模态框。
暂时的解决方法是使用内联导航栏模式:.navigationBarTitle(“Main”,displayMode:.inline)