8

As soon as I've added custom bar navigation bar button item I've lost the ability to use the default function to go back. I'd like to have the ability to use "swipe from edge" to go back.

I've added the Edge Pan Gesture Recogniser and connected it to @IBAction, but the dismissing action happens completely as soon as the pan gesture is recognised.

Instead of slowly following my thumb (as seen in other apps), the current view moves out with predefined animation.

How to make the animation following my thumb using Edge Pan Gesture Recogniser?

@IBAction func edgeSwipe(sender: AnyObject) {
    navigationController?.popViewControllerAnimated(true)
}

enter image description here

Andrej
  • 7,266
  • 4
  • 38
  • 57
  • 1
    Why is the system supplied gesture not working? Have you tried [this](http://stackoverflow.com/a/28919337/5442445)? – beyowulf Jul 08 '16 at 12:35
  • @beyowulf It's not working because I've added a button to the left bar button item. I've added a button here as I want to have access to some styling features of the UIButton. However adding a normal Bar Button Item (if you just want to customise the title) also makes the system supplied gesture to stop working. And for the second part: I've followed your link just now and I was able to gain back the same behaviour for swiping back as the default system implementation. – Andrej Jul 08 '16 at 13:20
  • @beyowulf , you pointed me in the right direction and I'd like to award you with a bounty. But the only way to do it is if you provide an answer. So if you provide at least just a simple answer you get a bounty, it's up to you :) – Andrej Jul 13 '16 at 07:08
  • nah, it's okay. Happy coding! – beyowulf Jul 13 '16 at 11:45

1 Answers1

8

There's no need to add Edge Pan Gesture Recogniser. Following @beyowulf's suggestions I've been able to implement the swipe to go back feature that behaves the same way as the default system implementation does - the views edge follows my thumb as I swipe it to dismiss it.

So I've removed the ScreenEdge Pan Gesture Recogniser from the storyboard and also removed the related @IBAction.

I've made my first view controller to be the delegate for interactivePopGestureRecognizer. Here's the code:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.interactivePopGestureRecognizer?.delegate = self
    }
}

extension ViewController: UIGestureRecognizerDelegate {

    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return navigationController?.viewControllers.count > 1 ? true : false
    }
}
Andrej
  • 7,266
  • 4
  • 38
  • 57
  • this is working for in 2020, but I'm curious to know how can we use it globally means I don't want to add in every navigation controller. – Sohaib Siddique Nov 06 '20 at 04:50