我在另一个 UIViewController
视图之上有一个 UIViewController
视图作为子视图/模态,比如子视图/模态应该是透明的,添加到子视图中的任何组件都应该是可见的。问题是,我有的是子视图显示黑色背景,而不是有明确的颜色。我试图使 UIView
作为一个清晰的颜色,而不是黑色的背景。有人知道它出了什么问题吗?谢谢你的建议。
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:NO];
Second ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.opaque = YES;
self.view.backgroundColor = [UIColor clearColor];
}
我解决了问题。它在 iPhone 和 iPad 上都运行良好。没有黑色背景的模态视图控制器只是清除颜色/透明。我唯一需要改变的是我把 UIModalPresentationFullScreen
换成了 UIModalPresentationCurrentContext
。多么简单啊!
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
注意: 如果您正在使用 navigationController
的 modalPresentationStyle
属性:
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
注意: 坏消息是上面的解决方案在 iOS7上不起作用。好消息是我修复了 iOS7的问题!我向别人求助,他是这么说的:
当以模态方式呈现视图控制器时,iOS 会在呈现期间从视图层次结构中删除视图控制器下面的视图控制器。虽然模态呈现的视图控制器的视图是透明的,但是在它下面除了应用程序窗口之外什么也没有,它是黑色的。IOS7引入了一种新的模态表示风格 UIModalPresentationCustom
,这使得 iOS7不会删除视图控制器下面的视图。但是,为了使用这种模态表示样式,您必须提供自己的转换委托来处理表示和取消动画。这在 WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218的“使用视图控制器的自定义过渡”演讲中有所概述,该演讲还涵盖了如何实现您自己的过渡委托。
您可以在 iOS7: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions中看到我对上述问题的解决方案