解决方案
创建一个 UINavigationController 扩展如下:
NavigationViewExtension.swift
import UIKit
extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
struct AView: View {
var body: some View {
NavigationLink {
BView()
} label: {
Text("打开B")
.font(.system(size: 30))
.foregroundColor(.white)
.frame(width: 200, height: 200)
.background(.red)
}
.navigationTitle("A")
}
}
struct BView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
NavigationLink {
CView()
} label: {
Text("打开C")
.font(.system(size: 30))
.foregroundColor(.white)
.frame(width: 200, height: 200)
.background(.red)
}
.navigationTitle("B")
.navigationBarBackButtonHidden()
.toolbar(content: {
ToolbarItem(placement: .topBarLeading) {
Button {
dismiss()
} label: {
Image(systemName: "arrowshape.turn.up.backward.fill")
.foregroundColor(.yellow)
}
}
})
}
}