6

When I swipe from left to right on navigation bar, my navigation controller pops a view controller. I already looked at this question so I know I can set ...

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

... but it just disables the swipe on the view below the navigation bar, not the bar itself.

My current solution is to manually find the gesture and disable it. Which works, but not sure if there's a better way. The navigation bar doesn't seem to have a property like interactivePopGestureRecognizer.

// This is inside a `UINavigationController` subclass.
for (UISwipeGestureRecognizer *gr in self.navigationBar.gestureRecognizers) {
    if ([gr isKindOfClass:[UISwipeGestureRecognizer class]] && gr.direction == UISwipeGestureRecognizerDirectionRight) {
        gr.enabled = NO;
    }
}
Community
  • 1
  • 1
Hlung
  • 13,850
  • 6
  • 71
  • 90

1 Answers1

3

The UIGestureRecognizerDelegate has a method called - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch. If you are able to point out if the touch's view is the UINavigationBar, just make it return "NO".

Such Like

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return (![[[touch view] class] isSubclassOfClass:[UIControl class]]); // UIControl is whatever as you like.
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • 2 days of effort, happily and easily resolved. Thank you. The only fact I would add is that (in my case) the NavigationController.interactivePopGestureRecognizer.delegate assignment and shouldReceiveTouch function are added to the parentViewContoller, not the activeViewContoller. – user216661 Jan 06 '15 at 06:57