正确的方法是禁用和启用选项卡栏控制器中导航控制器的向后滑动

时间:2016-10-08 14:39:30

标签: ios swift uinavigationcontroller uitabbarcontroller uigesturerecognizer

我有一个TabBarController,有三个标签。第一个和最后一个选项卡是导航控制器,我想要在其中启用滑动。中间选项卡只是标准viewController。默认情况下,似乎没有启用向后滑动。我想这是因为我在这两个navigationControllers中使用了后退按钮。我在两个导航控制器的根视图控制器中尝试了以下代码来启用它:

viewDidLoad

self.navigationController?.interactivePopGestureRecognizer?.delegate = self

并符合UIGestureRecognizerDelegate

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {

        if (self.navigationController?.viewControllers.count)! > 1 {
            return true
        }

        return false
    }

滑动恢复工作现在可以正常工作,但是如果其中一个视图控制器推动另一个顶部的UI,应用程序将全部冻结。在Swift中启用和禁用滑动的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

实现这一目标的两个步骤:

  1. UIGestureRecognizerDelegate添加到您的类继承
  2. viewDidLoad中添加以下内容:
  3. 启用向后滑动:

    navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    

    停用向后滑动:

    navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    

答案 1 :(得分:0)

For people like me who find this 2 years later without an answer

ViewController

In viewDidAppear :

self.navigationController?.interactivePopGestureRecognizer?.delegate = self

and your UIGestureRecognizerDelegate

extension YourViewController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    if gestureRecognizer.isEqual(navigationController?.interactivePopGestureRecognizer) {
        navigationController?.popViewController(animated: true)
    }
    return false
}