title: UIViewController – issue with custom dismiss transition
body: <h1>Summary</h1>
  
  <p>I have a content UIViewController that presents a settings UIViewController using a custom transition. The presentation is with <code>presentViewController:animated:completion:</code>.</p>
  
  <p>When I later dismiss the settings with <code>dismissViewControllerAnimated:completion:</code>, the presenting controller is suddenly jumped back to it's initial position prior to the settings controller presentation.</p>
  
  <p>I have a work around for this on the device but not the simulator. However, I'd like to know what I'm doing wrong rather than hack in a bodge that makes it go away. I also plan to make this animation interactive, and I suspect this issues will amplify when I do this.</p>
  
  <h1>Custom Transition – Opening the hood</h1>
  
  <p>The desired effect is that the <em>presenting</em> controller slides down the screen, and the <em>presented</em> controller is seen to be lying behind it from where it lifts up to fill the screen. The top of the presenting controller remains on-screen during the lifetime of use of the presented controller. It stays at the bottom of the screen, but <em>above</em> the presented controller.</p>
  
  <p>You could imagine lifting the bonnet on a car (the front presenting controller) to see the engine behind (the presented settings), but the bonnet stays visible at the bottom for a bit of context.</p>
  
  <p>I plan to refine this so that the presenting controller really appears to lift up with perspective in a 3d way, but I've not got that far, yet.</p>
  
  <p>When the settings are dismissed, the original presenting controller (bonnet) should slide back up the screen and the presented controller (settings) sink back slightly (closing the bonnet).</p>
  
  <h1>Code</h1>
  
  <p>Here's the method that toggles the settings on and off the screen (it's just called by a UIButton). You'll notice that the presenting view controller sets itself up as the <code>&lt;UIViewControllerTransitioningDelegate&gt;</code>.</p>
  
  <pre><code>-(void) toggleSettingsViewController
  {
    const BOOL settingsAreShowing = [self presentedViewController] != nil;
    if(!settingsAreShowing)
    {
      UIViewController *const settingsController = [[self storyboard] instantiateViewControllerWithIdentifier: @"STSettingsViewController"];
      [settingsController setTransitioningDelegate: self];
      [settingsController setModalPresentationStyle: UIModalPresentationCustom];
      [self presentViewController: settingsController animated: YES completion: nil];
    }
    else
    {
      [self dismissViewControllerAnimated: YES completion: nil];
    }
  }
  </code></pre>
  
  <p>To implement <code>&lt;UIViewControllerAnimatedTransitioning&gt;</code> the presenting view controller also just returns itself as the <code>&lt;UIViewControllerAnimatedTransitioning&gt;</code></p>
  
  <pre><code>-(id&lt;UIViewControllerAnimatedTransitioning&gt;) animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
  {
    return self;
  }
  
  -(id&lt;UIViewControllerAnimatedTransitioning&gt;) animationControllerForDismissedController:(UIViewController *)dismissed
  {
    // Test Point 1.
    return self;
  }
  </code></pre>
  
  <p>So finally, the presenting view controller will receive <code>animateTransition:</code>:</p>
  
  <pre><code>-(void) animateTransition:(id&lt;UIViewControllerContextTransitioning&gt;)transitionContext
  {
    UIViewController *const fromController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *const toController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  
    const BOOL isUnwinding = [toController presentedViewController] == fromController;
    const BOOL isPresenting = !isUnwinding;
  
    UIViewController * presentingController = isPresenting ? fromController : toController;
    UIViewController * presentedController = isPresenting ? toController : fromController;
  
    if(isPresenting)
    {
      // Add the presented controller (settings) to the view hierarchy _behind_ the presenting controller.
      [[transitionContext containerView] insertSubview: [presentedController view] belowSubview: [presentingController view]];
  
      // Set up the initial position of the presented settings controller. Scale it down so it seems in the distance. Alpha it down so it is dark and shadowed.
      presentedController.view.transform = CGAffineTransformMakeScale(0.9, 0.9);
      presentedController.view.alpha = 0.7;
  
      [UIView animateWithDuration: [self transitionDuration: transitionContext] animations:^{
        // Lift up the presented controller.
        presentedController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
  
        // Brighten the presented controller (out of shadow).
        presentedController.view.alpha = 1;
  
        // Push the presenting controller down the screen – 3d effect to be added later.
        presentingController.view.layer.transform = CATransform3DMakeTranslation(0,400,0);
       } completion: ^(BOOL finished){
         [transitionContext completeTransition: ![transitionContext transitionWasCancelled]];
       }];
    }
    else
    {
      // Test Point 2.
  
      // !!!This line should not be needed!!!
      // It resets the presenting controller to where it ought to be anyway.
      presentingController.view.layer.transform = CATransform3DMakeTranslation(0,400,0);
  
      [UIView animateWithDuration: [self transitionDuration: transitionContext] animations:^{
        // Bring the presenting controller back to its original position.
        presentingController.view.layer.transform = CATransform3DIdentity;
  
        // Lower the presented controller again and put it back in to shade.
        presentedController.view.transform = CGAffineTransformMakeScale(0.9, 0.9);
        presentedController.view.alpha = 0.4;
      } completion:^(BOOL finished) {
        [transitionContext completeTransition: ![transitionContext transitionWasCancelled]];
      }];
    }
  }
  
  -(NSTimeInterval) transitionDuration:(id&lt;UIViewControllerContextTransitioning&gt;)transitionContext
  {
    return 0.5;
  }
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>In the code above, I've indicated <strong>!!!This line should not be needed!!!</strong>.</p>
  
  <p>What's happening is that between <strong>Test Point 1</strong> and <strong>Test Point 2</strong> the screen position of the presenting view controller is reset to be the default full screen bounds. So, instead of being at the bottom of the screen ready to animate back up again smoothly, it suddenly jumps up the screen to position that it is meant to smoothly animate too!</p>
  
  <p>I've tried various approaches to animating the presenting view controller down the screen:</p>
  
  <ul>
  <li>I've changed its view's frame.</li>
  <li>I've changed its view's transform.</li>
  <li>I've changed its view's layer's 3d transform.</li>
  </ul>
  
  <p>In all cases, at <strong>Test Point 1</strong>, when the transition delegate is asked for, the presenting controller is set up as I would expect. However, in all cases, at <strong>Test Point 2</strong>, the presenting view controller has lost the correct position and has been "cleared" to have the normal full screen position that I want to animate it to.</p>
  
  <p>In the work around above I explicitly relocate the presenting view controller back to where it should be at the start of the animation with <strong>!!!This line should not be needed!!!</strong>. This seems to work on the device with the current version of iOS 7. However, on the simulator, the controller is visible at the cleared position for at least one frame.</p>
  
  <p>I am suspicious that I am doing something else wrong, and that I'm going to get in to trouble with my workaround just masking another problem.</p>
  
  <p>Any ideas what's going on? Thanks!</p>