InteractivePopGestureRecognizer 破坏了根视图控制器上的导航堆栈 [英] interactivePopGestureRecognizer corrupts navigation stack on root view controller

查看:25
本文介绍了InteractivePopGestureRecognizer 破坏了根视图控制器上的导航堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 UINavigationController 中,我添加了自定义后退按钮,其副作用是无法再从左向右滑动以弹出视图控制器并返回.

In my UINavigationController I added custom back buttons with the side effect that it is not possible anymore to swipe left to right to pop the view controller and navigate back.

所以我在我的自定义 UINavigationController 类中实现了 interactivePopGestureRecognizer:

So I implemented interactivePopGestureRecognizer in my custom UINavigationController class:

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)
    }
}

这很好用,除非我在我的根视图控制器 (RVC) 中,它是一个 UICollectionViewController,即导航堆栈中最底部的视图控制器.当我做从左到右的滑动手势时,似乎没有发生任何事情,正如预期的那样.但是当我点击 UICollectionViewCell 时,目标视图控制器 (DVC) 不会被推到 RVC 上.相反,我只能在屏幕右侧看到 DVC 的阴影.

This works fine except when I am in my root view controller (RVC) which is a UICollectionViewController, i.e. the most bottom view controller in the navigation stack. When I do the swipe left to right gesture, nothing seems to happen, as expected. But when I then tap a UICollectionViewCell the destination view controller (DVC) does not get pushed over the RVC. Instead I only see the DVC's shadow on the right side of the screen.

RVC 不再响应,但是当我再次从左向右滑动时,DVC 以交互方式从右向左移动到屏幕中.当我完成这个手势时,DVC 完全进入屏幕,只是为了再次从左到右快速消失.RVC 再次响应.

The RVC is not responsive anymore, but as I swipe left to right again, the DVC interactively moves right to left into the screen. When I finish the gesture, the DVC moves completely into the screen just to quickly disappear left to right again. The RVC becomes responsive again.

所以看起来 DVC 被推送到导航堆栈上,但并不明显地进入屏幕.

So it seems the DVC gets pushed onto the navigation stack but not visibly into the screen.

对于这种奇怪行为的起源有什么建议吗?

Any suggestions where this strange behaviour originates?

推荐答案

为导航控制器实现 UINavigationControllerDelegate 并在那里启用/禁用手势识别器.

Implement UINavigationControllerDelegate for your navigation controller and enable/disable the gesture recognizer there.

// 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
}

保持代码独立于推送的视图控制器.

Keeping the code independent from the pushed view controllers.

这篇关于InteractivePopGestureRecognizer 破坏了根视图控制器上的导航堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆