InteractivePopGestureRecognizer distorts the navigation stack on the root view controller

In mine, UINavigationControllerI added custom back buttons with a side effect that it is no longer possible to scroll left and right to pull out the view controller and go back.

So, I have implemented interactivePopGestureRecognizerin my custom class UINavigationController:

class UINavigationControllerExtended: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        if self.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
            self.interactivePopGestureRecognizer?.delegate = self
        }
    }

    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailByGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return gestureRecognizer.isKindOfClass(UIScreenEdgePanGestureRecognizer)
    }
}

, , (RVC), UICollectionViewController, . , , . UICollectionViewCell, (DVC) RVC. DVC .

RVC , -, DVC . , DVC , . RVC .

, , DVC , .

, ?

+4
3

- interactivePopGestureRecognizer :

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.navigationController?.interactivePopGestureRecognizer?.enabled = false
}

. , , , , , .

+2

UINavigationControllerDelegate / .

// Fix bug when pop gesture is enabled for the root controller
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
    self.interactivePopGestureRecognizer?.enabled = self.viewControllers.count > 1
}

.

+2

Swift 3
First, make a class for this:

import UIKit

class Total: NSObject,UIGestureRecognizerDelegate {

    //MARK:_Swip
    func backSwipVC(_ pr:UIViewController) {
        pr.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        pr.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }

}  

and name it in viewDidLoad () as follows:

Total().backSwipVC(self)
-1
source

Source: https://habr.com/ru/post/1623587/


All Articles