如何在iOS 7上的UINavigationController中禁用后滑动手势

本文翻译自:How to disable back swipe gesture in UINavigationController on iOS 7

In iOS 7 Apple added a new default navigation behavior. 在iOS 7中,Apple添加了一个新的默认导航行为。 You can swipe from the left edge of the screen to go back on the navigation stack. 您可以从屏幕的左边缘滑动以返回导航堆栈。 But in my app, this behavior conflicts with my custom left menu. 但在我的应用程序中,此行为与我的自定义左菜单冲突。 So, is it possible to disable this new gesture in UINavigationController? 那么,是否可以在UINavigationController中禁用这个新手势?


#1楼

参考:https://stackoom.com/question/1ACy4/如何在iOS-上的UINavigationController中禁用后滑动手势


#2楼

I found a solution: 我找到了解决方案:

Objective-C: Objective-C的:

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

Swift 3: 斯威夫特3:
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false


#3楼

I found out setting the gesture to disabled only doesn't always work. 我发现将手势设置为禁用只是并不总是有效。 It does work, but for me it only did after I once used the backgesture. 它确实有效,但对我而言,它只是在我曾经使用过背景后才做到的。 Second time it wouldn't trigger the backgesture. 第二次它不会触发反馈。

Fix for me was to delegate the gesture and implement the shouldbegin method to return NO: 修复我的是委托手势并实现shouldbegin方法返回NO:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Disable iOS 7 back gesture
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // Enable iOS 7 back gesture
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return NO;
}

#4楼

I've refined Twan's answer a bit, because: 我稍微改进了Twan的答案,因为:

  1. your view controller may be set as a delegate to other gesture recognisers 您的视图控制器可以设置为其他手势识别器的代理
  2. setting the delegate to nil leads to hanging issues when you go back to the root view controller and make a swipe gesture before navigating elsewhere. 将委托设置为nil会导致挂起问题,当您返回到根视图控制器并在导航到其他位置之前进行滑动手势。

The following example assumes iOS 7: 以下示例假设iOS 7:

{
    id savedGestureRecognizerDelegate;
}

- (void)viewWillAppear:(BOOL)animated
{
    savedGestureRecognizerDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.interactivePopGestureRecognizer.delegate = savedGestureRecognizerDelegate;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.navigationController.interactivePopGestureRecognizer) {
        return NO;
    }
    // add whatever logic you would otherwise have
    return YES;
}

#5楼

As of iOS 8 the accepted answer no longer works. 从iOS 8开始,接受的答案不再适用。 I needed to stop the swipping to dismiss gesture on my main game screen so implemented this: 我需要停止在我的主游戏屏幕上解除手势,所以实现了这个:

- (void)viewDidAppear:(BOOL)animated
{
     [super viewDidAppear:animated];

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }

}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
     return NO;
}

#6楼

EDIT 编辑

If you want to manage swipe back feature for specific navigation controllers, consider using SwipeBack . 如果要管理特定导航控制器的滑动功能,请考虑使用SwipeBack

With this, you can set navigationController.swipeBackEnabled = NO . 有了这个,你可以设置navigationController.swipeBackEnabled = NO

For example: 例如:

#import <SwipeBack/SwipeBack.h>

- (void)viewWillAppear:(BOOL)animated
{
    navigationController.swipeBackEnabled = NO;
}

It can be installed via CocoaPods . 它可以通过CocoaPods安装。

pod 'SwipeBack', '~> 1.0'

I appologize for lack of explanation. 我为缺乏解释而道歉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值