How to change animation style of modal UIViewController?

I am currently showing a UIViewController as follows:

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

and hide it as follows:

 [self.navigationController dismissModalViewControllerAnimated:YES]; 

The animation "moves down" ... then moves back. How to change the animation style? Can I make it fade in / out?

Hurrah!

+69
iphone cocoa-touch
Oct 26 '08 at 1:09
source share
4 answers

Marcus Zarra posted a great solution for 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 curling pages. If you are attenuated, you can try setting up a new alpha view:

 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 out. When the UINavigationController switches to the new view, it deletes the old one. For this effect, you're probably better off simply adding a new view to an existing UIViewController and fading out its alpha over time.

Note. If you are not in your application deletion, [self window] will not work. Use self.view.window, thanks to user412500 post to indicate this.

+60
Oct 26 '08 at 1:46
source share

For iPhone 3.0+, the basic crossfade is easiest to do as follows:

 modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [[self navigationController] presentModalViewController:modalViewController animated:YES]; 
+194
Jan 05 '10 at
source share

To update alpha attenuation 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;}]; 
+8
Oct 08 2018-10-10
source share

It must be [self.view.window] for the code to work

(at least as described in ios 3.2)

+2
02 Sep '10 at 23:41
source share



All Articles