如何在UINavigationBar上禁用滑动右手势?

时间:2014-02-18 10:09:41

标签: ios ios7 uinavigationcontroller uinavigationbar uigesturerecognizer

当我在导航栏上从左向右滑动时,我的导航控制器会弹出一个视图控制器。我已经看过this question所以我知道我可以设置......

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

...但它只是禁用导航栏下方视图的滑动,而不是栏本身。

我目前的解决方案是手动查找手势并禁用它。哪个有效,但不确定是否有更好的方法。导航栏似乎没有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;
    }
}

1 个答案:

答案 0 :(得分:3)

UIGestureRecognizerDelegate有一个名为- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch的方法。 如果您能够指出触摸的视图是UINavigationBar,只需返回“否”

喜欢

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return (![[[touch view] class] isSubclassOfClass:[UIControl class]]); // UIControl is whatever as you like.
}