Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable swipe right gesture on UINavigationBar?

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;
    }
}
like image 777
Hlung Avatar asked Feb 18 '14 10:02

Hlung


1 Answers

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.
}
like image 113
iPatel Avatar answered Oct 05 '22 20:10

iPatel