22

因此,我注意到当用户在屏幕的最左侧(任一方向)滑动时,我的所有视图都收到返回(弹出视图)的手势(这是 iOS7 的新功能

到目前为止,我已经尝试使用以下方法将其关闭,但无济于事:

    [self.navigationItem setHidesBackButton:YES];

在 NavigationController 本身的 init 中(因为委托似乎正在使用它)。

4

6 回答 6

72

对象-c

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

迅速

navigationController?.interactivePopGestureRecognizer?.isEnabled = false
于 2013-09-26T05:17:25.553 回答
6

添加到 Gabriele 的解决方案。

要支持 iOS 7 之前的任何 iOS,您需要使用以下代码包装此代码:

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

这将阻止应用程序在 iOS 6 和 iOS 5 中因缺少选择器而崩溃。

于 2013-11-19T00:50:09.603 回答
5

我在我的项目中使用此解决方案,它仅禁用 interactivePopGestureRecognizer 并允许您使用其他手势识别器。

- (void)viewDidLoad {

    [super viewDidLoad];

    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;

    }

}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {

        return NO;

    } else {

        return YES;

    }

}
于 2014-11-12T12:17:20.953 回答
4

我发现仅将手势设置为禁用并不总是有效。它确实有效,但对我来说,它只有在我使用过后手势之后才有效。第二次它不会触发背景手势。

对我来说,修复是委托手势并实现 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;
}
于 2014-04-24T14:32:12.250 回答
3

对于 IOS 8(斯威夫特):

class MainNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer.enabled = false

        // Do any additional setup after loading the view.
    }

}
于 2015-07-08T05:41:11.227 回答
0

将此代码用于 iOS 7 之前的版本

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
于 2014-02-07T10:48:03.353 回答