我有一个 UIViewController
View 作为另一个 UIViewController
View 之上的 subview /模态,例如 subview /模态应该是透明的,并且添加任何组件 subview 应该是可见的。问题是我的 subview 显示黑色背景而不是具有clearColor。我正在尝试将 UIView
设为 clearColor 而不是黑色背景。有人知道它有什么问题吗?任何建议表示赞赏。
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:NO];
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.opaque = YES;
self.view.backgroundColor = [UIColor clearColor];
}
已解决:我已解决问题。它适用于 iPhone 和 iPad。没有黑色背景的模态视图 Controller 只是 clearColor/transparent。我唯一需要更改的是将 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];
注意:坏消息是上述解决方案在 iOS 7 上不起作用。好消息是我为 iOS7 解决了这个问题!我向某人寻求帮助,他是这样说的:
当以模态方式呈现 View Controller 时,iOS 会在其呈现期间从 View 层次结构中移除其下方的 View Controller 。虽然模态显示的 View Controller 的 View 是透明的,但它下面除了黑色的应用程序窗口之外什么都没有。 iOS 7 引入了一种新的模态表示样式,UIModalPresentationCustom
,这会导致 iOS 不会删除呈现的 View Controller 下方的 View 。但是,为了使用这种模态演示样式,您必须提供自己的转换委托(delegate)来处理演示和关闭动画。这在来自 WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218 的“使用 View Controller 的自定义转换”演讲中进行了概述。其中还介绍了如何实现您自己的转换委托(delegate)。
您可能会在 iOS7 中看到我对上述问题的解决方案:https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions
最佳答案
iOS8+
在 iOS8+ 中,您现在可以使用新的 modalPresentationStyle UIModalPresentationOverCurrentContext 来呈现具有透明背景的 View Controller :
MyModalViewController *modalViewController = [[MyModalViewController alloc] init];
modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:modalViewController animated:YES completion:nil];
关于iphone - 在 UIViewController 上显示 clearColor UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11236367/