mtb mtb -3 years ago 99
iOS Question

iOS8 - Presenting a modal view removes sub view

I am updating our app to be compiled with xcode6/iOS8.

One issue I am running into is that when a modal view is presented. the underlying subview is removed. It is completely blacked out.. until the modal view is dismissed.. then it re-appears.

Has anyone run into this with iOS8? The same code has worked since iOS4.

Code:

PigDetailViewController *pigDetailViewController = [[PigDetailViewController alloc] initWithNibName:@"PigDetailViewController" bundle:nil];

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
self.navigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;


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

Answer Source

In iOS 8 they've added a new presentation style that behaves like UIModalPresentationCurrentContext in the circumstance you've described, it's UIModalPresentationOverCurrentContext. The catch here is that unlike with UIModalPresentationCurrentContext, you want to set the view controller to be PRESENTED with this presentation style, like so:

PigDetailViewController *pigDetailViewController = [[PigDetailViewController alloc] initWithNibName:@"PigDetailViewController" bundle:nil];
pigDetailViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
self.navigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:pigDetailViewController animated:YES completion:nil];

Note that to support iOS 7 and below you'll likely need to check the OS version and decide how to present the view controller based on that.

edit: I'd like to add one more note to this. In iOS7 with UIModalPresentationCurrentContext, when the presented VC was dismissed, the underlying VC had its viewDidAppear method called. In iOS8 with UIModalPresentationOverCurrentContext, I've found the underlying VC does not have its viewDidAppear method called when the VC presented over top of it is dismissed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download