I want to check whenever the user swipes a popped viewController away. So for example when in whatsApp a user exits the current chat by swiping from the edge. How is that possible in Swift?
I don’t want to use viewDidDisappear, because this method also gets called when another viewController is presented over the current viewController.
2
Answers
This "swipe" is handled by the
interactivePopGestureRecognizer
of theUINavigationController
. It is possible to set the delegate of this gesture recognizer to yourUIViewController
as follows:Then, you can implement the
UIGestureRecognizerDelegate
in yourUIViewController
. This would look like this:I haven’t tested the code, but this should print every time the interactivePopGestureRecognizer is used.
For more information, refer to the documentation of the interactivePopGestureRecognizer and UIGestureRecognizerDelegate.
As I wrote in comment, a simple workaround would be in
viewDidDisappear
, check if thenavigationController
isnil
.Of course, this solution works only if the view controller is embedded into a navigation controller, otherwise the if statement will always be
true
.