在我的 UINavigationController
中,我添加了自定义后退按钮,其副作用是无法再从左向右滑动以弹出 View Controller 并向后导航。
所以我在自定义 UINavigationController
类中实现了 interactivePopGestureRecognizer
:
class UINavigationControllerExtended: UINavigationController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if self.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
self.interactivePopGestureRecognizer?.delegate = self
}
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailByGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return gestureRecognizer.isKindOfClass(UIScreenEdgePanGestureRecognizer)
}
}
这工作正常,除非我在我的 Root View Controller (RVC) 中,它是一个 UICollectionViewController
,即导航堆栈中最底部的 View Controller 。当我做从左向右滑动的手势时,似乎没有发生任何事情,正如预期的那样。但是,当我随后点击 UICollectionViewCell 时,目标 View Controller (DVC) 不会被推到 RVC 上。相反,我只在屏幕右侧看到 DVC 的阴影。
RVC 不再有响应,但当我再次从左向右滑动时,DVC 交互地从右向左移动到屏幕中。当我完成手势时,DVC 完全移入屏幕,然后再次从左到右快速消失。 RVC 再次响应。
所以看起来 DVC 被推到导航堆栈上,但没有明显地进入屏幕。
有什么关于这种奇怪行为起源的建议吗?
最佳答案
为您的导航 Controller 实现 UINavigationControllerDelegate
并在那里启用/禁用手势识别器。
// Fix bug when pop gesture is enabled for the root controller
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
self.interactivePopGestureRecognizer?.enabled = self.viewControllers.count > 1
}
保持代码独立于推送的 View Controller 。
关于swift - interactivePopGestureRecognizer 损坏 Root View Controller 上的导航堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34698018/