Search code examples
iosuiviewcontrolleruinavigationcontrollerpresentviewcontroller

Black screen when dismissing a UIViewController with custom transition from within a UINavigationController


I have a view controller FromViewController and I want it to present a ToViewController with a custom transition. I have implemented the UIViewControllerAnimatedTransitioning for both the to- and from-transitions and it's all working nicely when I present and dismiss the ToViewController.

However, if my FromViewController is contained within a UINavigationController I just get a black screen when I dismiss the ToViewController.

This is the code from the UIViewControllerAnimatedTransitioning when dismissing:

- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
    self.toViewController = (ToViewController *)
    [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UINavigationController *navigationController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    self.fromViewController = (FromViewController *)navigationController.topViewController;
    self.containerView = transitionContext.containerView;

    [self.containerView insertSubview:self.fromViewController.view belowSubview:self.toViewController.view];

    // My animations go here

    // In the animation completion block I call:
    [transitionContext completeTransition:!transitionContext.transitionWasCancelled];  // this is when my screen goes black
}

I have inspected the app and it seems that neither the UINavigationController nor FromViewController are in the view hierarchy after the dismissal.


Solution

  • I figured it out. The problem was when I was adding the view to the transition context container view:

     [self.containerView insertSubview:self.fromViewController.view belowSubview:self.toViewController.view];
    

    In stead I needed to add the view from the navigation controller:

    [self.containerView insertSubview:navigationController.view belowSubview:self.toViewController.view];