iosswiftuinavigationcontrolleruiviewanimationtransitioncustom-transition

Inside UIViewControllerAnimatedTransitioning delegate, the transitionContext.fromViewController is nil


I have a UINavigationController setup and am expecting to use a custom animation for pop / push of views with it. I have used custom transitions before without issue, but in this case I am actually finding nil values in my 'from' and 'to' UIViewControllers.

My setup is very similar to this SO Post

Custom DataEntryViewController

class DataEntryViewController : UIViewController, DataEntryViewDelegate, UINavigationControllerDelegate {

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {        
        let animator = DataEntryTransitionAnimator()
        animator.duration = 2
        return animator
    }
}

Custom BaseTransitionAnimator

class BaseTransitionAnimator : NSObject, UIViewControllerAnimatedTransitioning {

    var duration : NSTimeInterval = 0.5             // default transition time of 1/2 second
    var appearing : Bool = true                     // is the animation appearing (or disappearing)

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return duration
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        assert(true, "animateTransition MUST be implemented by child class")
    }
}

Subclassed TransitionAnimator

class DataEntryTransitionAnimator : BaseTransitionAnimator {

    override func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewKey) as! DataEntryViewController
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewKey) as! DataEntryViewController
        let duration = transitionDuration(transitionContext)

        // do fancy animations
    }
}

Using the above, both fromVC and toVC are nil

How is it possible that the transitionContext doesn't have valid pointers to the 'to' and 'from' UIViewControllers?


Solution

  • you are using UITransitionContextFromViewKey but you need to use UITransitionContextFromViewControllerKey

    same for "to"