我在另一个UIViewController
视图之上有一个作为子视图/模式的UIViewController
视图,比如子视图/模式应该是透明的,添加到子视图中的任何组件都应该是可见的。问题是,我有是子视图显示黑色背景,而不是有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上都工作得很好。没有黑色背景的模式视图控制器只有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的问题!我向某人寻求帮助,他是这样说的:
当以模态方式呈现视图控制器时,iOS会在呈现期间从视图层次结构中删除它下面的视图控制器。虽然以模态方式呈现的视图控制器的视图是透明的,但它下面除了应用程序窗口之外什么都没有,它是黑色的。iOS 7引入了一种新的模式表示样式UIModalPresentationCustom
__,它使得iOS不会删除所呈现的视图控制器下面的视图。但是,为了使用这种模式表示样式,您必须提供自己的转换委托来处理表示和取消动画。这在WWDC2013 的“使用视图控制器自定义过渡”演讲中概述了这一点,其中还介绍了如何实现自己的过渡代理。
您可以在iOS7中查看我对上述问题的解决方案:https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions
https://stackoverflow.com/questions/11236367
复制相似问题