12

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.

aks.knit1108
  • 1,305
  • 9
  • 20
zerotalent
  • 121
  • 5

5 Answers5

2

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];
Muddu Patil
  • 713
  • 1
  • 11
  • 24
Mohd Iftekhar Qurashi
  • 2,385
  • 3
  • 32
  • 44
1

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];
Julian Osorio
  • 1,116
  • 1
  • 12
  • 30
  • This is wrong! If you use UIModalPresentationCurrentContext the underlying viewController is still visible! Try it by presenting a slightly transparent viewController! – Alexander Jul 30 '14 at 09:04
  • @Alexander I tried what you said and it didn't work. can you provide some code? I am talking that the transparency did not appear to me – Julian Osorio Jul 30 '14 at 19:32
1

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

Community
  • 1
  • 1
Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97
1
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

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

navController.modalPresentationStyle = UIModalPresentationCurrentContext;

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

initiate viewController with storyboard identifier, it may help

1

Let me know if this helps, the current view controller will be dismissed in order for the animation to start.

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

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

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;

[self dismissViewControllerAnimated:YES completion:^{
  [self presentViewController:navController animated:YES completion:nil];
}];
edwardlayys
  • 79
  • 10