Exit navigationItem by swiping anywhere on the screen does not work as expected when returning from non navigation controller viewcontrollers

Viewed 78

I add the following code to every VCTest:

    let popGestureRecognizer = self.navigationController!.interactivePopGestureRecognizer!
    if let targets = popGestureRecognizer.value(forKey: "targets") as? NSMutableArray {
      let gestureRecognizer = UIPanGestureRecognizer()
      gestureRecognizer.setValue(targets, forKey: "targets")
      self.view.addGestureRecognizer(gestureRecognizer)
    }

This allows me to swipe from anywhere on the screen back to the base viewcontroller (child of navigation controller) from anywhere on the screen.

When however I leave to a non-navigationcontroller controller, and then return, swiping on the screen all of a sudden does not work. Litteraly nothing is interactable.

Why is this happening and how can I solve this?

1 Answers

interactivePopGestureRecognizer docs:

The navigation controller installs this gesture recognizer on its view and uses it to pop the topmost view controller off the navigation stack

So basically this works only for ViewControllers inside of UINavigationController stack. What you can do is create another panGestureRecognizer for non-navigation controllers:

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(dismissController))
view.addGestureRecognizer(panGestureRecognizer)

@objc
func dismissController() {
    dismiss(animated: true)
}
Related