UINavigation Handle specific swipe back for some ViewController

782 Views Asked by At

In my current project, I use one main UINavigationController and I've pushed every controller that I want to present. Also, I've enabled the pop gesture from the default user experience for UINavigationController that when I swipe on the edge left side of the screen, it'll pop my view controller and everything is working fine.

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
self.navigationController?.interactivePopGestureRecognizer?.delegate = self

extension MyCustomViewController: UIGestureRecognizerDelegate{
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return gestureRecognizer.isKind(of: UIScreenEdgePanGestureRecognizer.self)
    }
}

But in some case, I need to handle a specific action when I swipe my ViewController. Instead of pop my ViewController, I want to use this function:

extension UINavigationController {
    func backToViewController(vc: Swift.AnyClass) {
        for element in viewControllers as Array {
            if element.isKind(of: vc) {
                self.popToViewController(element, animated: true)
                break
            }
        }   
    }
}
0

There are 0 best solutions below