69

I'm currently displaying a UIViewController like this:

[[self navigationController] presentModalViewController:modalViewController animated:YES];

and hiding it like this:

[self.navigationController dismissModalViewControllerAnimated:YES];

The animation is "slide up from the bottom"... then slide back down. How can I change the animation style? Can I made it fade in/out?

Cheers!

Jason
  • 28,040
  • 10
  • 64
  • 64
MrDatabase
  • 43,245
  • 41
  • 111
  • 153

4 Answers4

194

For iPhone 3.0+, a basic crossfade is easiest to do like this:

modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[[self navigationController] presentModalViewController:modalViewController
                                               animated:YES];
Jason
  • 28,040
  • 10
  • 64
  • 64
Simo Salminen
  • 2,306
  • 2
  • 14
  • 11
  • 2
    Works perfectly!!! It took me a couple of seconds to realize that the transition has to be applied to the viewController that is being presented and not self. – looneydoodle Apr 20 '11 at 04:42
  • You can do this in Interface Builder, by adding a modal segue and setting the transition to `Cross Dissolve`, for example. – paulvs Aug 26 '15 at 21:02
60

Marcus Zarra posted a great solution to this on the SDK mailing list:

UIViewController *controller = [[[MyViewController alloc] init] autorelease];
UIViewAnimationTransition trans = UIViewAnimationTransitionCurlUp;
[UIView beginAnimations: nil context: nil];
[UIView setAnimationTransition: trans forView: [self window] cache: YES];
[navController presentModalViewController: controller animated: NO];
[UIView commitAnimations];

There are transitions for flipping and page-curling. If you are set on fading, can try adjusting your new view's alpha:

UIViewController *controller = [[[MyViewController alloc] init] autorelease];
controller.view.alpha = 0.0;
[navController presentModalViewController: controller animated: NO];
[UIView beginAnimations: nil context: nil];
controller.view.alpha = 1.0;
[UIView commitAnimations];

However, what you probably want is a crossfade, or at least a fade-over. When the UINavigationController switches to a new view, it removes the old one. For this effect, you're probably better off just adding a new view to your existing UIViewController and fading its alpha in over time.

Note: If you are not in your app delegate [self window] will not work. Use self.view.window , thanks to user412500's post for pointing this out.

Sheepdogsheep
  • 417
  • 4
  • 13
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
8

To update for alpha fading in iOS 4:

modalController.view.alpha = 0.0;
[self.view.window.rootViewController presentModalViewController:modalController animated:NO];
[UIView animateWithDuration:0.5
                 animations:^{modalController.view.alpha = 1.0;}];
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
2

It should be [self.view.window] in order for the code to work

(at least that's the way that it is in ios 3.2)

Mark Coleman
  • 40,542
  • 9
  • 81
  • 101
user412500
  • 57
  • 3