UIModalPresentationCurrentContext with Transition?

别说谁变了你拦得住时间么 提交于 2019-11-28 19:45:14

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...

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'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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!