我有3个窗口通过NavigationLink连接,NavigationBar是隐藏的,但我需要滑动才能返回,为此我使用以下代码:
import SwiftUI
extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
@main
struct testSheetApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
NavigationView{
ZStack{
VStack{
NavigationLink(destination: {
ContentView2()
}, label: {
Text("new")
})
}
} .navigationBarHidden(true)
}
}
}
struct ContentView2: View {
@Environment(.presentationMode) var presentationMode
var body: some View {
ZStack{
VStack{
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("back")
})
NavigationLink(destination: {
ContentView3()
}, label: {
Text("next")
})
}
} .navigationBarHidden(true)
}
}
struct ContentView3: View {
@Environment(.presentationMode) var presentationMode
var body: some View {
VStack{
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("back")
})
}
.navigationBarHidden(true)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
第二个表单一切正常,您可以滑动返回,但在第三个表单上,当您取消滑动时,当屏幕保持在同一个表单上时,导航栏会出现
Bug导航栏
我找到了这个问题的解决方案,您需要添加到NavigationLink中,"。isDetailLink(false(">
代码片段:
NavigationLink(destination: {
ContentView3()
}, label: {
Text("next")
}).isDetailLink(false)