我有以下代码在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];
Run Code Online (Sandbox Code Playgroud)
然后我在后台视图控制器的viewWillAppear中有以下内容,将其视图恢复为完全不透明.
[UIView animateWithDuration:1.0 animations:^(void) {
self.view.alpha = 1.0;
[self.navigationController.view setAlpha:1.0];
}];
Run Code Online (Sandbox Code Playgroud)
现在,使用iOS8,上面的代码不会将背景设置为半透明.OverlayViewController环绕黑色.
我在网上发现使用UIModalPresentationOverCurrentContext会给出预期的行为.它实际上确实如此,但背景视图控制器永远不会脱离视图层次结构(编辑以添加对此行为的引用:https://developer.apple.com/documentation/uikit/uimodalpresentationstyle).因此,从不调用viewWillAppear,因此永远不会删除半透明度.
显然,我可以使用像使用NSNotificationCenter的黑客,并在删除OverlayViewController时触发通知,但感觉就像一个简单的迂回方式.有没有其他方式我可以优雅地实现这一目标?
推论问题:
1)如果UIModalPresentationOverCurrentContext是实现这一目标的唯一方法,那么我想知道是否会被迫放置两个版本的代码以使其在iOS7和iOS8中都能正常工作.
2)显然,在早期版本的Xcode中无法识别新的枚举.那么,我的团队是否应该升级到Xcode 6以确保他们可以运行此代码,即使他们的其余工作仅以iOS7为中心?或者有没有办法告诉旧版本的Xcode忽略仅适用于iOS8的特定代码块?