首页 > 解决方案 > 提供自定义动画控制器时,UINavigationController 中的系统交互弹出手势中断

问题描述

我有一个自定义子类,UINavigationController它将自己设置为UINavigationControllerDelegate,并有条件地返回一个自定义动画师。我希望能够使用布尔标志在自定义动画师和系统动画之间切换。我的代码看起来像这样:

class CustomNavigationController: UINavigationControllerDelegate {

    var useCustomAnimation = false
    private let customAnimator = CustomAnimator()

    func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationController.Operation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if useCustomAnimation {
            return CustomAnimator()
        }
        return nil
    }
}

但是,当useCustomAnimationis时false,系统管理的交互式返回手势不再起作用。与系统动画相关的所有其他内容仍然有效。

我尝试将交互式弹出手势的委托设置为我的自定义导航控制器,并从某些成功程度不同的方法返回真/假。

标签: iosswiftuinavigationcontrolleruikit

解决方案


所以这似乎是 UIKit 中的一个错误。我创建了一个重现错误并将其提交给 Apple 的小项目。本质上,只要animationController代理方法由UINavigationControllerDelegate. 作为一种解决方法,我创建了两个委托代理,一个实现该方法,一个不实现:

class NavigationControllerDelegateProxy: NSObject, UINavigationControllerDelegate {

    weak var delegateProxy: UINavigationControllerDelegate?

    init(delegateProxy: UINavigationControllerDelegate) {
        self.delegateProxy = delegateProxy
    }

    /*
    ... Other Delegate Methods
    */
}

class CustomAnimationNavigationControllerDelegateProxy: NavigationControllerDelegateProxy {

    func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationController.Operation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return delegateProxy?.navigationController?(navigationController,
                                                    animationControllerFor: operation,
                                                    from: fromVC,
                                                    to: toVC)
    }
}

我只是UINavigationControllerDelegate根据useCustomAnimation.


推荐阅读