在iOS 7中,苹果增加了一个新的默认导航行为。你可以从屏幕的左边缘滑动回到导航堆栈。但在我的应用程序中,这种行为与我的自定义左侧菜单冲突。那么,是否有可能在UINavigationController中禁用这个新手势?


当前回答

self.navigationController.pushViewController(VC, animated: Bool)

call

self.navigationController.setViewContollers([VC], animated: Bool)

setViewControllers替换堆栈上的所有VCs,而不是在顶部添加一个新的控制器。这意味着新的集合VC是根VC,用户不能返回。

当你只想在一个VC上禁用滑动,而在另一个VC上保持滑动时,这是最有效的。

如果你想让用户能够返回,而不是通过滑动,不要使用这个方法,因为它会禁用所有返回(因为没有VC可以返回)

其他回答

它适用于我的ios 10和更高版本:

- (void)viewWillAppear:(BOOL)animated {
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }

}

它在viewDidLoad()方法上不起作用。

迅速:

navigationController!.interactivePopGestureRecognizer!.enabled = false

我找到了一个解决方案:

objective - c:

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

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

这适用于viewDidLoad:

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

在dispatch_after的帮助下,很多问题都可以解决。

尽管请注意,这种解决方案可能不安全,但请使用自己的推理。

更新

对于iOS 8.1,延迟时间应该是0.5秒

在iOS 9.3上不再需要延迟,它只需要将这个放在你的viewDidLoad中: (iOS 9.0-9.3版本待定)

navigationController?.interactivePopGestureRecognizer?.enabled = false

我的方法。一个手势识别器来统治它们:

class DisabledGestureViewController: UIViewController: UIGestureRecognizerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController!.interactivePopGestureRecognizer!.delegate = self
    }

    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        // Prevent going back to the previous view
        return !(navigationController!.topViewController is DisabledGestureViewController)
    }
}

重要:不要在导航堆栈的任何地方重置委托:navigationController!.interactivePopGestureRecognizer!.delegate = nil