3

I am trying to create custom animation during transition between two uiviewcontrollers. When i am trying to pop a view controller i am getting nil object in [transitionContext containerView] . Here is my code for custom transition class

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController * toViewController   = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromViewController = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *containerView = [transitionContext containerView];//returns nil
    [containerView addSubview:toViewController.view];
    [fromViewController.view removeFromSuperview];
    [transitionContext completeTransition:YES];
}

I am using similar kind of code while pushing the view controller and its working perfectly. But during pop its creating trouble.

Jay Gajjar
  • 2,661
  • 2
  • 21
  • 35

1 Answers1

1

I was running into this issue myself, and it looks like you need to add your fromViewController's view to containerView as well.

As the documentation for the transitionContext's containerView method says:

The animator object is responsible for adding the view of the presented view controller...

It's unclear why, but not adding your fromViewController's view (the "presented view controller" view) to the containerView results in a working transition the first time, but a nil containerView on subsequent transitions.

Walter
  • 221
  • 2
  • 5
  • Isn't the "presented view controller" the toViewController? This seems to suggest you don't add the fromViewController: http://stackoverflow.com/questions/24830258/container-view-disappearing-on-completetransition ETA: I just noticed the original question is iOS7. My comments is iOS8+ I believe, based on that link. – chadbag Jan 29 '16 at 18:25