UINavigationController Interactive Pop Gesture Not Working?

前端 未结 10 1436
你的背包
你的背包 2020-12-13 10:02

So I have a navigation controller in my built for iOS 7 app. The titleView is visible, as well as the back button and navigation bar its self. For some reason, the interac

相关标签:
10条回答
  • 2020-12-13 10:34

    My answer is based on Eneko's answer but uses only an extension on UINavigationController and works in Swift 5:

    extension UINavigationController: UIGestureRecognizerDelegate {
    
        override open func viewDidLoad() {
            super.viewDidLoad()
            interactivePopGestureRecognizer?.delegate = self
        }
    
        public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            return viewControllers.count > 1
        }
    }
    
    0 讨论(0)
  • 2020-12-13 10:35

    You can put this line in the viewDidLoad method.

    self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
    
    0 讨论(0)
  • 2020-12-13 10:35

    Generically add interactive pop gesture to the whole app.

    XCODE: 9.0, Swift: 4.0

    Preferably create UINavigationController in AppDelegate.swift

    1. Create a navigation controller
    // I created a global variable, however not necessarily you will be doing this way
    var nvc: UINavigationController!
    
    1. implement UIGestureRecognizerDelegate
    class AppDelegate: UIResponder, UIApplicationDelegate, UIGestureRecognizerDelegate {
    
    1. Instantiat UINavigationController in application didFinishLaunchingWithOptions function
    nvc=UINavigationController()
    
    // For interactive pop gesture
    nvc.navigationBar.isHidden=true
    nvc?.interactivePopGestureRecognizer?.delegate=self
    
    1. Extra step, add controller to navigation controller in application didFinishLaunchingWithOptions function
    window=UIWindow()
    window?.rootViewController=nvc
    window?.makeKeyAndVisible()
    
    // BaseViewController is sample controller i created with xib
    nvc.pushViewController(BaseViewController(), animated: true)
    
    1. Implement gusture recognizer, add below code to AppDelegate.swift
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
            return true
        }
    



    Note: See other post in this section for the difference between

    self.navigationController?.navigationBar.isHidden=true
    

    And

    self.navigationController?.isNavigationBarHidden = true
    
    0 讨论(0)
  • 2020-12-13 10:36

    If you feel you have tried all solutions and stretching your head then you're at the right place.

    Goto simulator > Window > Enable Show Device Bezels
    

    Now tried to simulate swipe to back gesture.

    0 讨论(0)
提交回复
热议问题