Swipe to go back only works on edge of screen?

后端 未结 3 1058
失恋的感觉
失恋的感觉 2020-12-10 22:48

My swipe to go back feature works but only works on the edge of the screen. How can I have it work from anywhere on the screen?

3条回答
  •  执笔经年
    2020-12-10 23:14

    It's actually quite easy to do on the UINavigationController subclass without any intervention into every UIViewController subclass pushed. Also respecting built-in swipe-from-edge state (so when it's disabled intentionally, the new gesture is disabled as well):

    import UIKit
    
    class NavigationController: UINavigationController {
        override func viewDidLoad() {
            super.viewDidLoad()
            setupFullWidthBackGesture()
        }
    
        private lazy var fullWidthBackGestureRecognizer = UIPanGestureRecognizer()
    
        private func setupFullWidthBackGesture() {
            // The trick here is to wire up our full-width `fullWidthBackGestureRecognizer` to execute the same handler as
            // the system `interactivePopGestureRecognizer`. That's done by assigning the same "targets" (effectively
            // object and selector) of the system one to our gesture recognizer.
            guard
                let interactivePopGestureRecognizer = interactivePopGestureRecognizer,
                let targets = interactivePopGestureRecognizer.value(forKey: "targets")
            else {
                return
            }
    
            fullWidthBackGestureRecognizer.setValue(targets, forKey: "targets")
            fullWidthBackGestureRecognizer.delegate = self
            view.addGestureRecognizer(fullWidthBackGestureRecognizer)
        }
    }
    
    extension NavigationController: UIGestureRecognizerDelegate {
        func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            let isSystemSwipeToBackEnabled = interactivePopGestureRecognizer?.isEnabled == true
            let isThereStackedViewControllers = viewControllers.count > 1
            return isSystemSwipeToBackEnabled && isThereStackedViewControllers
        }
    }
    

提交回复
热议问题