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

时间:2013-06-20 09:01:46

标签: ios objective-c uinavigationcontroller uigesturerecognizer ios7

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

16 个答案:

答案 0 :(得分:568)

我找到了解决方案:

目标C:

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

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

答案 1 :(得分:45)

我发现将手势设置为禁用只是并不总是有效。它确实有效,但对我而言,它只是在我曾经使用过背景后才做到的。第二次它不会触发反馈。

修复我的方法是委托手势并实现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;
}

答案 2 :(得分:26)

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

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    [self.navigationController.view removeGestureRecognizer:self.navigationController.interactivePopGestureRecognizer];

答案 3 :(得分:21)

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

答案 4 :(得分:18)

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

  1. 您的视图控制器可能被设置为其他手势识别器的代理
  2. 将代理设置为nil会导致挂起问题,当您返回到根视图控制器并在导航到其他位置之前进行滑动手势。
  3. 以下示例假设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 :(得分:10)

请在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;
}

答案 6 :(得分:8)

对于Swift:

navigationController!.interactivePopGestureRecognizer!.enabled = false

答案 7 :(得分:6)

修改

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

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

例如:

#import <SwipeBack/SwipeBack.h>

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

可以通过CocoaPods安装。

pod 'SwipeBack', '~> 1.0'

我为缺乏解释而道歉。

答案 8 :(得分:5)

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

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

}

它不适用于viewDidLoad()方法。

答案 9 :(得分:3)

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

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

答案 10 :(得分:3)

这是Swift 3的方式

适合我

    self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false

答案 11 :(得分:2)

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

在viewcontroller中将private var popGesture: UIGestureRecognizer?声明为全局变量。然后在 viewDidAppear viewWillDisappear 方法中实现代码

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 之后向后滑动

答案 12 :(得分:2)

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

[navigationController.interactivePopGestureRecognizer requireGestureRecognizerToFail: myPanGestureRecognizer];

其中myPanGestureRecognizer是您正在使用的手势识别器,例如显示你的菜单。这样,当您推动新的导航控制器时,Apple的手势识别器并没有被他们拒之门外,而且您不需要依赖可能过早发射的笨拙延迟,如果您的手机是睡觉或重负荷。

离开这里是因为我知道下次需要时我不记得这个,然后我会在这里解决问题。

答案 13 :(得分: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
    }

}

答案 14 :(得分:0)

它对大多数视图控制器都有效。

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
}

答案 15 :(得分:0)

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

// disable
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
// enable
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true