10

I am trying to modal present a view controller like below:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"addPopover"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:vc animated:YES];

Now using UIModalPresentationCurrentContext means I can present this view with a transparent background and see my other view behind the new one. However, it stops me from being able to present it with a transition.

Any ideas why? Or how I can get around this? Thanks.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253

3 Answers3

25

I was able to accomplish this by setting modalPresentationStyle = UIModalPresentationCurrentContext on the rootViewController of my UIWindow, IF I haven't presented any new full screen viewControllers on top of this rootViewController. I did something like this:

UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:YES completion:nil];

I've tried this with both a UINavigationController as my window's rootViewController and various other custom view controllers. It seems as long as the window's rootViewController knows what's going on, any sub-viewControllers can call presentViewController:animated:completion: without blacking-out the underlying view.

However, let's say you present another viewController on top of your window's rootViewController. And this new viewController is presented with modalPresentationStyle = UIModalPresentationFullScreen (ie. takes up the screen), then you have to call modalPresentationStyle = UIModalPresentationCurrentContext on that top-most viewController.

So to recap:

  • If you have UINavigationController -> UIViewController(s) (they could be pushed and popped in and out), then you set modalPresentationStyle = UIModalPresentationCurrentContext on the UINavigationController.
  • If you have UINavigationController -> UIViewController -> new-UIViewController (with modalPresentationStyle set to UIModalPresentationFullScreen), then you set modalPresentationStyle = UIModalPresentationCurrentContext on the new-UIViewController.

Hopefully this works for you guys as well!

Mr. T
  • 12,795
  • 5
  • 39
  • 47
  • 1
    This worked for me. The key was setting the modalPresentationStyle on the presenting view controller, not the view controller being presented. – Scott Ahten Dec 13 '12 at 20:58
  • What if you are presenting it on a UISplitViewController? – Kyle Clegg Dec 19 '12 at 23:10
  • 1
    I'm getting a blackout background as soon as the animation finished. I'm second view controller on a stack (on top of course) of a `UINavigationController` I've tried setting `modalPresentationStyle = UIModalPresentationCurrentContext` on the vc to appear, `self`, `self.navigationController` and even `appDelegate.window.rootViewController` to no avail. Obviously I'm missing something though, any ideas? – DBD Jan 16 '13 at 19:14
  • Another problem with this approach is that the hole rotation breaks. – Ecarrion Apr 12 '13 at 16:41
  • hey it's works but i have another issue with rotation. parent viewcontroller is not rotating according to modal viewcontroller rotation – karan Dec 05 '13 at 06:41
  • 1
    This is not working for me in iOS 7. iOS 8 its fine. – abc123 Jan 18 '15 at 18:51
11

Full screen modals aren't supposed to allow you to see the under layer. Annoying I know.

From your code I'm assuming that "addPopover" is actually a full screen view, where you have your content as a subsection while the rest is transparent.

Try this:

vc.modalPresentationStyle = UIModalPresentationCurrentContext;
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:vc animated:YES completion:NULL];

Note: I'd recommend adding a low alpha background color to let the user know that they can't interact with the rest of the view when you pop this over top...

Michael Kernahan
  • 1,442
  • 1
  • 13
  • 15
  • 1
    What's different here? Setting modalTransitionStyle on the target view didn't make any difference and neither did using presentViewController over presentModalViewController. – Marcus Adams Oct 18 '12 at 19:17
  • assigning the transition style to `vc` instead of `self`, and using `presentViewController` method. Without seeing your storyboard it's hard to diagnose if this doesn't work. With the above method, your overlaid VC needs to be the same size as "self" (or be able resizing) – Michael Kernahan Oct 19 '12 at 15:17
  • 2
    Yes, in my project, both ViewControllers are the same size. I tried your code and there was no change. The property setting `modalPresentationStyle = UIModalPresentationCurrentContext` needs to be applied to self here, not vc. I guess I will have to start my own question instead of offering bounty on this one because there is no solution here, even though it's marked as one. – Marcus Adams Oct 19 '12 at 17:07
  • Link it here and I'll try to check it out. Sorry this didn't help you. – Michael Kernahan Oct 19 '12 at 18:13
  • But 'PresentingViewController' (View controller that presents new view controller) wont get rotated. Is there any way to do that? – Rafeek Oct 18 '13 at 10:57
0

Mr. T's suggestions worked nearly perfectly, you just lose the transition animation on the presented viewcontroller

Since it is also setting the appDelegates rootviewController's presentation style, it will also remove the transition animations for any views presented from that point.

My fix was to return the AppDelegates rootViewController's presentation style back to it's default whenever the viewcontroller is closed that I needed the transparent background on

I have a button that dismisses the presentedViewController, in that I also set the presentation style of the rootViewController back to default using:

    UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;

I hope that helps anyone else getting stumped on this problem.

Derek Hewitt
  • 775
  • 8
  • 16