我有一个自定义的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
}
}
但是,当useCustomAnimation
false
时,系统管理的交互式后退手势不再起作用。与系统动画相关的其他所有内容仍然有效。
我尝试将交互式弹出手势的委托设置为我的自定义导航控制器,并从某些方法返回 true/false,成功程度各不相同。
所以这似乎是UIKit中的一个错误。我创建了一个重现该错误的小项目并将其提交给Apple。本质上,每当UINavigationControllerDelegate
实现animationController
委托方法时,交互式弹出手势都会被破坏。作为解决方法,我创建了两个委托代理,一个实现该方法,另一个不实现:
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)
}
}
我只是根据useCustomAnimation
的状态在这些类之间交替充当实际UINavigationControllerDelegate
。