在我的 SwiftUI 應用程式中,我已在所有視圖.navigationBarBackButtonHidden(true)中設定始終有一個自定義后退按鈕,因此 iOS 經典的滑動回傳已被禁用,但我實際上只在特定視圖中需要它。
我使用以下代碼重新啟用它:
extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
它可以作業,但不幸的是,它重新啟用滑動以回傳整個專案,我該怎么辦?
uj5u.com熱心網友回復:
它可以通過一些全域應用程式狀態進行管理,例如
class AppState {
static let shared = AppState()
var swipeEnabled = false // << by default
}
extension UINavigationController: UIGestureRecognizerDelegate {
// ...
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return AppState.shared.swipeEnabled ?
viewControllers.count > 1 : false // << here !!
}
}
// ... and somewhere in view, for example
.onAppear {
AppState.shared.swipeEnabled = true
}
.onDisappear {
AppState.shared.swipeEnabled = false
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/471906.html
