我有一个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中启用和禁用滑动的正确方法是什么?
答案 0 :(得分:1)
实现这一目标的两个步骤:
UIGestureRecognizerDelegate
添加到您的类继承viewDidLoad
中添加以下内容:要启用向后滑动:
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
}