338

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

4

18 回答 18

609

我找到了一个解决方案:

目标-C:

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

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

于 2013-06-20T12:27:17.767 回答
50

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

对我来说,修复是委托手势并实现 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:28:32.487 回答
30

只需从 NavigationController 中删除手势识别器。在 iOS 8 中工作。

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    [self.navigationController.view removeGestureRecognizer:self.navigationController.interactivePopGestureRecognizer];
于 2015-08-19T13:43:57.920 回答
23

从 iOS 8 开始,接受的答案不再有效。我需要在我的主游戏屏幕上停止滑动以关闭手势,所以实现了这个:

- (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;
}
于 2015-02-01T17:50:52.317 回答
20

我已经完善了 Twan 的答案,因为:

  1. 您的视图控制器可以设置为其他手势识别器的委托
  2. nil当您返回根视图控制器并在导航到其他地方之前做出滑动手势时,将委托设置为会导致挂起问题。

以下示例假设 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;
}
于 2014-12-22T10:22:24.253 回答
11

请在root vc中设置:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;

}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:YES];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
于 2017-04-04T05:43:34.383 回答
11

对于斯威夫特:

navigationController!.interactivePopGestureRecognizer!.enabled = false
于 2016-11-02T09:33:09.233 回答
6

swift 5, swift 4.2 可以使用下面的代码。

// disable
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
// enable
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
于 2019-12-18T09:35:08.173 回答
5

编辑

如果您想管理特定导航控制器的向后滑动功能,请考虑使用SwipeBack

有了这个,你可以设置navigationController.swipeBackEnabled = NO.

例如:

#import <SwipeBack/SwipeBack.h>

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

它可以通过CocoaPods安装。

pod 'SwipeBack', '~> 1.0'

我为缺乏解释而道歉。

于 2015-02-06T16:50:12.930 回答
5

它适用于 ios 10 及更高版本:

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

}

它不适用于 viewDidLoad() 方法。

于 2017-01-03T10:23:27.487 回答
4

这是 Swift 3 的方式

为我工作

    self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
于 2017-03-13T11:50:04.360 回答
4

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

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

于 2016-10-28T08:55:08.220 回答
3

所有这些解决方案都以他们不推荐的方式操纵 Apple 的手势识别器。我刚刚被一个朋友告诉我有一个更好的解决方案:

[navigationController.interactivePopGestureRecognizer requireGestureRecognizerToFail: myPanGestureRecognizer];

其中 myPanGestureRecognizer 是您用来显示菜单的手势识别器。这样一来,当您按下新的导航控制器时,Apple 的手势识别器不会被他们重新打开,并且您无需依赖在手机进入睡眠状态或负载过重时可能会过早触发的 hacky 延迟。

把它留在这里是因为我知道下次我需要它时我不会记住它,然后我会在这里找到问题的解决方案。

于 2016-10-26T14:16:30.577 回答
2

这适用于viewDidLoad:iOS 8:

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

很多问题都可以在好 ol' 的帮助下解决dispatch_after

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

更新

对于 iOS 8.1 延迟时间应为 0.5 秒

在 iOS 9.3 上不再需要延迟,只需将其放在您的viewDidLoad: 中
即可工作(如果适用于 iOS 9.0-9.3,则待定)

navigationController?.interactivePopGestureRecognizer?.enabled = false
于 2015-09-09T21:55:09.250 回答
2

给出的答案都没有帮助我解决问题。在这里发布我的答案;可能对某人有帮助

在视图控制器中声明private var popGesture: UIGestureRecognizer?为全局变量。然后在viewDidAppearviewWillDisappear方法中实现代码

override func viewDidAppear(animated: Bool) {

    super.viewDidAppear(animated)

    if self.navigationController!.respondsToSelector(Selector("interactivePopGestureRecognizer")) {

        self.popGesture = navigationController!.interactivePopGestureRecognizer
        self.navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer!)
    }
}


override func viewWillDisappear(animated: Bool) {

    super.viewWillDisappear(animated)

    if self.popGesture != nil {
        navigationController!.view.addGestureRecognizer(self.popGesture!)
    }
}

这将在 iOS v8.x及更高版本中禁用滑动

于 2016-07-05T11:54:46.337 回答
1

它适用于大多数视图控制器。

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false

它不适用于像 UIPageViewController 这样的视图控制器。在 UIPageViewController 的 pagecontentviewcontroller 上,下面的代码对我有用。

override func viewDidLoad() {
   self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
   self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}
override func viewWillDisappear(_ animated: Bool) {
   self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
   self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
}

在 UIGestureRecognizerDelegate 上,

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
   if gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer {
      return false
}
      return true
}
于 2019-03-14T07:51:39.583 回答
1

对于Swift 4这有效:

class MyViewController: UIViewController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.interactivePopGestureRecognizer?.gesture.delegate = self
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)

        self.navigationController?.interactivePopGestureRecognizer?.gesture.isEnabled = false
    }

}
于 2018-01-20T15:01:41.520 回答
0
self.navigationController.pushViewController(VC, animated: Bool)

称呼

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

setViewControllers 替换堆栈上的所有 VC,而不是在顶部添加新控制器。这意味着新设置的 VC 是根 VC,用户无法返回。

当您只想禁用单个 VC 上的滑动并为其他 VC 保留滑动到背面时,这是最有效的。

如果您希望用户能够返回,而不是通过滑动,请不要使用此方法,因为它会禁用所有返回(因为没有 VC 可以返回)

于 2021-10-08T07:07:04.350 回答