RÉPONSE: simple erreur de syntaxe - lors du réglage du délégué, il faut lire SomeNavigationController.delegate = auto, pas SomeNavigationController.navigationController .delegate = autoComment connecter le UINavigationControllerDelegate au contrôleur de navigation?
Ci-dessous le contrôleur de vue de base où le contrôleur de navigation est créé?. Je me suis déclaré en tant que délégué et je me suis conformé au protocole du délégué dans une extension à ce contrôleur de vue et j'ai créé un objet en tant qu'animateur personnalisé. Mais les contrôleurs de vue dans cette pile de navigation n'adoptent pas cette animation; Qu'est-ce que je rate?
Un code doit-il exister dans le contrôleur de vue racine de cette pile de navigation ou dans les contrôleurs de vue qui sont poussés et déplacés dans cette pile? Ou tout le code d'animation personnalisé appartient-il ici (dans le contrôleur de vue où le contrôleur de navigation est créé) et il me manque juste quelque chose?
View Controller où UINavigationController est créé
class BaseViewController: UIViewController {
override func loadView() {
buildNavigationController()
}
func buildNavigationController() {
let SomeNavigationControllerRoot = SomeViewController()
let SomeNavigationController = UINavigationController(rootViewController: SomeNavigationControllerRoot)
SomeNavigationController.delegate = self
view.addSubview(SomeNavigationController.view)
}
}
extension BaseViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomAnimator()
}
}
class CustomAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 1.0
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
containerView.addSubview(toViewController!.view)
toViewController!.view.alpha = 0.0
UIView.animate(withDuration: 1.0, animations: {
toViewController!.view.alpha = 1.0
}, completion: { finished in
let cancelled = transitionContext.transitionWasCancelled
transitionContext.completeTransition(!cancelled)
})
}
}
Racine View Controller dans UINavigationController pile
class SomeViewController: UIViewController, UINavigationControllerDelegate {
func pushToNext() {
let randomViewController = randomViewController()
navigationController?.pushViewController(randomViewController, animated: true)
}
}
Je suppose au lieu de délégué déléguer la transition ensemble. toViewController.transitioningDelegate = auto. –