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.
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];
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];
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With