1

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
            }
        }   
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anonymous-E
  • 827
  • 11
  • 29
  • Why do you need a custom gesture recognizer for swipe back? UINavigationController already has a pop on swipe gesture? – Joshua Francis Roman May 03 '19 at 03:31
  • @JoshuaFrancisRoman because default pop swipe gesture just pop one ViewController from UINavigation Stack but In my case, I want to pop 2 or 3 ViewController instead, that's why I use a function call 'backToViewController' – Anonymous-E May 03 '19 at 03:38
  • Have you tried using `UINavigationControllerDelegate`s `navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool)` ? I believe you could handle it from there rather than custom gestures. – Joshua Francis Roman May 03 '19 at 03:43
  • I don't think it would work in this case or you can show me a little bit more of that idea, **For reminder: If I have 3 Viewcontrollers in Navigation Stack Vc1 -> Vc2 -> vc3, I can go back to Vc1 from Vc3 by just call `self.navigationController?.backToViewController(vc: vc1.self)` and It's working fine but It can't be done for pop swipe back if I can't handle swipe edge gesture from default UINavigation** – Anonymous-E May 03 '19 at 04:16
  • Why don't you try with Segue and Unwind Segue concepts? – Bhavik Modi May 03 '19 at 04:33
  • @BhavikModi thanks for that concepts, I think it will work but because each view controller I implement it as Xib file instead of the storyboard. Also, I think the idea of Unwind Segue is the same as my `backToViewController(vc: )` – Anonymous-E May 03 '19 at 04:59
  • 1
    Yes @Anonymous-E, With XIB you can not use Segue/Unwind segue, No, using Unwind segue you can directly navigate back to defined view controller and you can perform any operation before unwind too in performSegue(). – Bhavik Modi May 03 '19 at 08:26
  • @BhavikModi oh! thanks, I did understand that idea too it's just my application already moved any viewcontroller to xib right now because have many viewcontroller in the storyboard is too messed up to another programmer. So I thought maybe we could have another way for this issue. Btw thank for your time – Anonymous-E May 03 '19 at 08:37

0 Answers0