如何在隐藏导航栏时启用滑动手势?

时间:2018-02-23 18:48:44

标签: ios swift uinavigationcontroller uinavigationbar uigesturerecognizer

我一直试图解决这个问题已经有一段时间了,并且无法弄明白。我有当前的设置:

enter image description here

在每个视图控制器中,我都隐藏了导航栏:

        self.navigationController?.setNavigationBarHidden(true, animated: true)

问题是我在视图控制器上松开了导航栏隐藏的滑动手势。我需要启用动画,不能使用:

    self.navigationController?.navigationBar.isHidden = true
    self.navigationController?.isNavigationBarHidden = true 

任何帮助都会很棒,因为我相信很多人都遇到过这个问题。谢谢!

2 个答案:

答案 0 :(得分:4)

以下是答案:只需将您的NavigationController子类化并执行以下操作。

  import UIKit

class YourUINavigationController: UINavigationController {
        override func viewDidLoad() {
            super.viewDidLoad()
            interactivePopGestureRecognizer?.delegate = self
        }
    }

    extension VaultUINavigationController: UIGestureRecognizerDelegate {
        func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            return viewControllers.count > 1
        }
    }

答案 1 :(得分:1)

您可以通过执行以下操作来处理滑动手势,它可以帮助您避免冻结应用程序。

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        if (self.navigationController.viewControllers.count > 1)
        {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
        else
        {
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}