I had the following code that worked perfectly in iOS7.
[UIView animateWithDuration:0.5 animations:^(void) {
self.view.alpha = 0.5;
[self.navigationController.navigationBar setAlpha:0.3];
}]; //to make the background view controller semi-transparent
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
[rootViewController setModalPresentationStyle:UIModalPresentationCurrentContext];
OverlayViewController *ctlr = [storyBoard instantiateViewControllerWithIdentifier:@"OverlayViewController"];
//present the OverlayViewController
[self presentViewController:ctlr animated:YES completion:nil];
Then I had the following in viewWillAppear of the background view controller, to revert back its view to full opaque.
[UIView animateWithDuration:1.0 animations:^(void) {
self.view.alpha = 1.0;
[self.navigationController.view setAlpha:1.0];
}];
Now, with iOS8, the above code doesn't set the background to be semi-transparent. Black color surrounds the OverlayViewController.
I found online that using UIModalPresentationOverCurrentContext will give the expected behavior. It does actually, but the background view controller is never taken off the view hierarchy (edited to add reference to this behavior: https://developer.apple.com/documentation/uikit/uimodalpresentationstyle). So, the viewWillAppear is never called, and so the semi-transparency is never removed.
Obviously, I can resort to hacks like using NSNotificationCenter and fire off a notification when the OverlayViewController is removed, but it feels like a roundabout way to do what should be simple. Is there any other way in which I can gracefully achieve this?
Corollary questions:
1) If UIModalPresentationOverCurrentContext is the only way to achieve this, then I am wondering if I would be forced to put two versions of the code to get it working both in iOS7 and iOS8.
2) Obviously that new enum is not recognized in earlier versions of Xcode. So, should my team upgrade to Xcode 6 just to ensure they can run this code, even if the rest of their work is centered around iOS7 only? Or is there any way of telling the older version of Xcode to ignore the particular code block that is needed only for iOS8?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…