In the new iOS 7 UINavigationController
, there is a swipe gesture to switch between views.
Is there a way to detect or intercept the gesture?
Asked
Active
Viewed 1.0k times
14
-
3`I won't disable it`. You make it sound like you're being "naughty" – James Webster Dec 17 '13 at 20:54
-
It's just because I read a similar topics about "how to disable swipe gesture in uinavigationcontroller". So I wanted to be clear ^^ – Steven Dec 18 '13 at 21:16
2 Answers
34
The interactive pop gesture recognizer is exposed through UINavigationController
's interactivePopGestureRecognizer
property. You can add your own controller as a target of the gesture recognizer and respond appropriately:
@implementation MyViewController
...
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.interactivePopGestureRecognizer addTarget:self
action:@selector(handlePopGesture:)];
}
- (void)handlePopGesture:(UIGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
// respond to beginning of pop gesture
}
// handle other gesture states, if desired
}
...
@end

Austin
- 5,625
- 1
- 29
- 43
14
Here is Austin's answer, in Swift. Using this post to construct the selector, I found the following to work.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.interactivePopGestureRecognizer?.addTarget(self, action:#selector(self.handlePopGesture))
}
func handlePopGesture(gesture: UIGestureRecognizer) -> Void {
if gesture.state == UIGestureRecognizerState.Began {
// respond to beginning of pop gesture
}
}
-
1this is not responding for me... i just wanted to print "hello" on gesture recognize – Nandkishor mewara May 26 '17 at 13:16
-
2I had to add `@objc` in front of `func handlePopGesture` to make it valid code. – lenooh Oct 26 '18 at 22:51