self.navigationController.interactivePopGestureRecognizer.enabled = NO;不适用于iOS 8

时间:2014-09-29 11:04:22

标签: ios objective-c xcode ios8

我想在滑动视图控制器时禁用弹出手势,但以下行在iOS 8中不起作用:

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

先谢谢

4 个答案:

答案 0 :(得分:4)

在您希望禁用它的viewcontroller中,添加以下行:

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

答案 1 :(得分:2)

我在我的项目中使用此解决方案,它仅禁用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;

    }

}

答案 2 :(得分:0)

以下解决方案!!!

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

      __weak id weakSelf = self;
      self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;  }

导航控制器在其视图上安装此手势识别器,并使用它将最顶层的视图控制器弹出导航堆栈。您可以使用此属性检索手势识别器,并将其与用户界面中其他手势识别器的行为联系起来。将手势识别器绑在一起时,请确保他们同时识别手势,以确保您的手势识别器有机会处理该事件。

通常我们需要在手动添加导航栏或我们自定义导航控制器时实施

Swift版本

在RootViewController上添加协议UIGestureRecognizerDelegate和viewWillAppear add:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.interactivePopGestureRecognizer!.delegate =  self }

答案 3 :(得分:-5)

- (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    });

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    });
}

我认为简单的方法就在上面。 您不应该直接在viewDidLoad()中启用手势。 但是你可以在 viewDidAppeared()之后改变它的状态。你知道它是之后,: - D