Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

iphone - UIModalPresentationCurrentContext with Transition?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...