How to resolve the swiping right action when trying to navigate to a previous screen in iOS

Daniel Torres
2 min readJun 21, 2022

Are you not being able to use the swipe right behavior when navigating back in your app? In this article I’ll show how to resolve this using the interactivePopGestureRecognizer from the UINavigationController when the top UIViewController has a scrollView in its view.

One of the reasons your app doesn’t recognize and cannot go back it’s because the interactivePopGestureRecognizer is not being used while other gesture is happening in the same time. The interactivePopGestureRecognizer is the responsible to manage the swipe right from the edge of the screen and to pop up the top view controller inside your navigation controller’s stack.

From the docs:

var interactivePopGestureRecognizer: UIGestureRecognizer?
// The gesture recognizer responsible for popping the top view controller off the navigation stack.

To help the interactivePopGestureRecognizer to recognize another gesture at the same time we have to make its delegate UIGestureRecognizerDelegate that two gesture recognizers should be allowed to recognize gestures simultaneously.

We can do this by using func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWith: UIGestureRecognizer) -> Bool

From the docs:

You can use this property (interactivePopGestureRecognizer) to retrieve the gesture recognizer and tie it to the behavior of other gesture recognizers in your user interface. When tying your gesture recognizers together, make sure they recognize their gestures simultaneously to ensure that your gesture recognizers are given a chance to handle the event.

The problem is that this delegate function returns false by default. So by returning a simply true in this function we can have the swipe right to return gesture back.

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

But what if the top view controller has a scrollview inside its view? In this case both, the swipe right to go back and the scrolling gestures will be recognized. What we need to do is to check if the other gesture recognizer (otherGestureRecognizer) is a UIPanGestureRecognizer which is the gesture that the scrollView and if it is then return false so we don’t recognize these gestures simultaneously.

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
guard (otherGestureRecognizer as? UIPanGestureRecognizer) != nil else {
return true
}

return false
}

Conclusion

We saw how to resolve the problem of the swiping back gesture to go back in an iOS app. This is done by setting the delegate from interactivePopGestureRecognizer of the UINavigationController we’re using and implementing its func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWith: UIGestureRecognizer) -> Bool delegate method.

--

--

Daniel Torres

iOS developer - Delivering exceptional results no matter the situation.