Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No animation when modalPresentationStyle is set to UIModalPresentationCurrentContext

When I'm presenting my UIViewController with modalPresentationStyle of the parent UINavigationController set to UIModalPresentationCurrentContext, the UIViewController is not sliding in. There is no transition used.

Here is my code:

UIViewController *viewController = [[UIViewController alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
navController.navigationBarHidden = YES;

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;

[self presentViewController:navController animated:YES completion:nil];

When I do not set the modalPresenttionStyle, everything works fine. But I need this style, because I want the UIViewController presents as overlay.

BTW: When the ViewController is dismissed, the animation works fine.

like image 679
zerotalent Avatar asked Feb 17 '13 15:02

zerotalent


3 Answers

According to the UIViewController.h header definition:-

/* Defines the transition style that will be used for this view controller when it is presented modally. Set this property on the view controller to be presented, not the presenter. Defaults to UIModalTransitionStyleCoverVertical. */

So you should apply this on the presentingViewController like this:-

UIViewController *viewController = [[UIViewController alloc] init];

 UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
 navController.navigationBarHidden = YES;

//Here is the change
 navController.modalPresentationStyle = UIModalPresentationCurrentContext;

[self presentViewController:navController animated:YES completion:nil];
like image 156
Mohd Iftekhar Qurashi Avatar answered Nov 05 '22 01:11

Mohd Iftekhar Qurashi


If you want the UIViewController to be presented as an overlay that is not the right approach because when you do: [self presentViewController:navController animated:YES completion:nil]; You are doing a modal presentation and you won't have the parent view controller below your current one. Instead you will have the UIWindow, so it will be probably black there and that is not what you want.

So in order to do what you want, you need to present your controller as a childViewController and add its view to your parent controller view like this:

UIViewController *viewController = [[UIViewController alloc] init];

[self addChildViewController:viewController];
[self viewWillDisappear:animated];
[self.view addSubview:viewController.view];
[self.view bringSubviewToFront:viewController.view];
[viewController didMoveToParentViewController:parentController];
[self viewDidDisappear:animated];

And to remove the UIViewController:

[controller.view removeFromSuperview];
[controller willMoveToParentViewController:nil];
[controller.parentViewController viewDidAppear:animated];
[controller removeFromParentViewController];
like image 29
Julian Osorio Avatar answered Nov 05 '22 02:11

Julian Osorio


If you want to add an overlay the first thing you need to do is ensure that you are using the new ViewController transition API's of iOS 7. Here's a quick tutorial Objc.io View Controller Transitions

Once you finish you should have an animator and a viewcontroller that conforms to the UIViewControllerTransitioningDelegate protocol.

Then when you want to present your controller you need to set the modal presentation style to UIModalPresentationStyleCustom and not CurrentContext. Naturally your animator will need to configure the frame of the presented controller so that you can still see the contents underneath.

Here's another tutorial that might help - Custom presentations

Last but not least you are going to have to handle the scenario of presentations in any orientation, if you don't you will see odd behaviour when rotating because the container of the transition remains in portrait. See my answer here - transitions in any orientation

like image 1
Daniel Galasko Avatar answered Nov 05 '22 00:11

Daniel Galasko