我有一个作为另一个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上不起作用。好消息是,我已经为iOS 7修复了这个问题!我向某人寻求帮助,他给出了以下建议:
当以模态方式呈现视图控制器时,iOS会从视图层次结构中删除其下面的视图控制器,以便在其呈现期间使用。虽然您模态呈现的视图控制器的视图是透明的,但除了应用程序窗口(黑色)之外,下面没有任何内容。 iOS 7引入了一种新的模态呈现样式UIModalPresentationCustom
,它使iOS不会删除呈现的视图控制器下面的视图。但是,为了使用此模态呈现样式,您必须提供自己的转换委托来处理呈现和解除动画。在“使用视图控制器进行自定义转换”WWDC 2013演讲中,概述了如何实现自己的转换委托。链接地址:https://developer.apple.com/wwdc/videos/?id=218
您可以在iOS 7中查看我针对上述问题的解决方案:https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions
modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
将解决这个问题。 - GoodSp33dmodalPresentationStyle
属性必须在呈现视图控制器之前设置:(Swift)presentedViewController.modalPresentationStyle = .overCurrentContext; self.present(presentedViewController, animated: true, completion: nil)
。 - Laurent Maquet