I am presenting a model view with animation. As a default it comes from bottom to top. How can I make the animation to go from left to right? I know I could use a navigation controller. But actually the presenting view does not need a navigation bar and also the modally presented view does not need a navigation bar. Still I want a transition from left to right.
-
1Possible duplicate of [dismissModalViewController with transition: left to right](https://stackoverflow.com/questions/11412467/dismissmodalviewcontroller-with-transition-left-to-right) – Duncan Babbage Oct 04 '17 at 07:30
-
A possible duplicate with additional information for dismissing the modalViewController with a reverse right to left animation as well :- https://stackoverflow.com/questions/11412467/dismissmodalviewcontroller-with-transition-left-to-right – ScorpionKing2k5 Apr 27 '13 at 21:45
6 Answers
You can animate from right to left while presenting a view controller by using the following code
CATransition *transition = [CATransition animation];
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:transition forKey:nil];
[self presentViewController:localitiesView animated:NO completion:nil];

- 3,433
- 2
- 28
- 38

- 4,133
- 2
- 19
- 12
-
5Awesome Solution, this has been working really good for me but I've bumped into one issue. Do you have any idea why I would be getting a black flash as the view animates in? – Unome Oct 18 '15 at 07:51
There are only four UIModalTransitionStyle
s:
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl
Like you said, a nav controller will push that way. If you don't want to use that, you'll have to animate the view yourself.

- 22,245
- 10
- 71
- 97
-
2I have not done it, but basically you would instantiate the viewController, set its `hidden` property to `YES`, present the modal controller, move its `frame.origin.x` to `-320` (in portrait mode), set its `hidden` property to `NO`, begin your animation chunk in which you set the `frame.origin.x` to 0 over a period of `1.0` or `1.5` seconds, and then start the animation. – Matthew Frederick Dec 27 '10 at 20:42
I ran into a problem implementing Matthew's solution because the view presenting my modal view would not be visible during the animation (instead the presenting view would be replaced with the Window background and the modal view would then animate over that) which led to a jarring experience. Instead I've added the modal view as a subview to the presenting UIViewController's view and animated it across. I had a different animation requested so I've tried to change some of the values to represent the animation you're describing but I have not actually tested the code below.
UIViewController *modalView = //init your UIViewController
[modalView loadView];
CGRect finalFrame = modalView.view.frame;
[modalView.view setFrame:CGRectMake(-320, 0, self.view.frame.size.width, self.view.frame.size.height)];
[UIView animateWithDuration:1.0 animations:^{
[self.navigationController setNavigationBarHidden:YES];
[self.view addSubview:modalView.view];
[modalView.view setFrame:finalFrame];
}];
Hope this helps.

- 1,589
- 1
- 11
- 21
UINavigationController
has a navigationBarHidden
property—if you set that to YES, you can get the left-to-right transition style and the other niceties of a navigation controller without having a visible navigation bar.

- 57,021
- 16
- 130
- 131
Just wanted to show another option - this one lets you present the view from top to bottom, using a UIView animation. please note you have to add the new view in hidden state - ensuring the animation starts with the view fully loaded.
AddViewController *controller = [[AddViewController alloc] initWithNibName:@"AddViewController" bundle:[NSBundle mainBundle]];
controller.blogs = self.blogs;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
navController.toolbarHidden = YES;
navController.navigationBarHidden = YES;
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:navController animated:NO];
navController.view.frame = CGRectMake(0, -480, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20);
navController.view.hidden = NO;
[UIView animateWithDuration:0.3
animations:^{
navController.view.frame = CGRectMake(0, 20, navController.view.frame.size.width, navController.view.frame.size.height);
}];

- 299
- 2
- 8
You can try something like this:
UIViewController *fooViewController = [[[UIViewController alloc] init] autorelease];
CGSize theSize = CGSizeMake(320, 460);
fooViewController.view.frame = CGRectMake(0 - theSize.width, 0, theSize.width, theSize.height);
[UIView beginAnimations:@"animationID" context:NULL];
fooViewController.view.frame = CGRectMake(0, 0, 320, 460);
[UIView commitAnimations];

- 1,187
- 12
- 28
-
Thanks for the answer. It gives me two compile errors. Also I am not quite sure what this is doing. – hol Dec 27 '10 at 21:56
-
It's doing what I suggested earlier: creates a viewController, sets its frame to be exactly its width to the left of the screen, starts an animation chunk that will last 1 second, tells the UIKit animator that it should move the viewController to the right until it fills the screen during that 1 second, and then performs the animation. What are your compile errors? Odds are you don't want an empty viewController like this creates, instead substituting in your viewController's class for `UIViewController` both places in the first line. – Matthew Frederick Dec 28 '10 at 00:44
-
On above 5th statement: invalid conversion initializing integer 'int', expected block pointer. On above 7th statement: lvalue required as left operand of assignment – hol Dec 28 '10 at 18:57