我有下面这段代码,在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];
然后我在背景视图控制器的viewWillAppear方法中添加了以下内容,将其视图恢复为完全不透明。
[UIView animateWithDuration:1.0 animations:^(void) {
self.view.alpha = 1.0;
[self.navigationController.view setAlpha:1.0];
}];
在 iOS8 中,以上代码无法将背景设置为半透明。黑色颜色会围绕 OverlayViewController。
我在网上发现了使用 UIModalPresentationOverCurrentContext 将会得到期望的行为。实际上确实是这样的,但是背景视图控制器永远不会从视图层级中删除 (编辑以添加对此行为的参考:https://developer.apple.com/documentation/uikit/uimodalpresentationstyle)。所以,viewWillAppear 永远不会被调用,因此半透明永远不会被移除。
显然,我可以诉诸于使用 NSNotificationCenter 等 hack 方法,并在 OverlayViewController 被移除时触发通知,但这感觉像是一个迂回的方式来完成应该简单的事情。是否有其他方法优雅地实现这一点呢?
相关问题:
1)如果 UIModalPresentationOverCurrentContext 是实现这一点的唯一方法,那么我想知道是否会被迫放两个版本的代码,以使其在 iOS7 和 iOS8 中都能正常工作。
2)很明显,旧版 Xcode 不认识这个新枚举类型。那么,我的团队应该升级到 Xcode 6,以确保他们可以运行这段代码,即使他们的其他工作只集中在 iOS7 上?还是有办法告诉旧版本的 Xcode 忽略仅针对 iOS8 需要的特定代码块呢?