Hlung
Hlung

Reputation: 14298

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;
    }
}

Upvotes: 6

Views: 746

Answers (1)

iPatel
iPatel

Reputation: 47049

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.
}

Upvotes: 3

Related Questions