5

I'm trying to make a custom segue so that the destination view controller slides down from the top.

I wrote my code according to the example from the documentation.

The problem is that when the segue is executed, the source view controller goes black, and then the animation occurs. How can I prevent the source view controller from going black?

(I already tried implementing the solution presented in this answer but the screen either goes black after the transition, or reverts to the source view controller.)

Here's my code:

-(void)perform{

    UIViewController *splashScreen = self.sourceViewController;
    UIViewController *mainScreen = self.destinationViewController;

    //Place the destination VC above the visible area        
    mainScreen.view.center = CGPointMake(mainScreen.view.center.x,
                                          mainScreen.view.center.y-600);

    //Animation to move the VC down into the visible area
    [UIView animateWithDuration:1
                     animations:^{
                         mainScreen.view.center = CGPointMake(mainScreen.view.center.x, [[UIScreen mainScreen] bounds].size.height/2 );
                     }
     ];

    [splashScreen presentModalViewController:mainScreen animated:NO];
}
Community
  • 1
  • 1
Eric
  • 16,003
  • 15
  • 87
  • 139
  • The solution is incredibly simple. You must finish the animation BEFORE you present the new view controller. There's nothing more to it. – Fattie Jun 21 '16 at 14:25

1 Answers1

3

The reason that your source view controller seems to be hidden is that the destination view controller is presented right away.

When you are writing custom segues you don't have both views available at the same time. You could either

  • push view controller, add the source view to the destination view controller and animate
  • add the destination view to the source view controller and animate, then push view controller
  • push to an in-between view controller, add both views, animate, push to the destination view controller.

In all the above cases where I say push view controllers you could instead present view controllers modally. In fact that might be more suitable for the in-between view controller solution.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • I tried the second option you mention, and it works fine. Here's the code: https://gist.github.com/3206340 But which of the three options you mention is most efficient? – Eric Jul 30 '12 at 11:30
  • There is not much difference in efficiency between them. You should choose the one that feels cleanest, easiest to understand and maintain for you. For production code I would personally choose the third option since it won't mess with the view hierarchy of either the source or the destination during the animation. But it's a matter of taste. – David Rönnqvist Jul 30 '12 at 11:42
  • @DavidRönnqvist I'm attempting a pop segue rather than a push segue, but I'm essentially following option 1, and my source view is not animating. Could you take a look at the code in [my question](http://stackoverflow.com/questions/13036014/view-transition-doesnt-animate-during-custom-pop-segue)? – glorifiedHacker Oct 23 '12 at 18:23