在UIViewController上显示clearColor UIViewController

我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];


注意:坏消息是上述解决方案无法在iOS 7上运行。好消息是我已修复iOS7的问题!我向某人求助,这是他说的话:


当以模态方式显示视图控制器时,iOS在显示期间将其下方的视图控制器从视图层次结构中移除。虽然以模态形式显示的视图控制器的视图是透明的,但在其下方除了黑色的应用程序窗口外没有其他任何东西。iOS 7引入了一种新的模式表示样式,UIModalPresentationCustom该样式使iOS不会删除所显示视图控制器下方的视图。但是,为了使用这种模式表示样式,必须提供自己的过渡委托来处理表示并关闭动画。WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218的“使用视图控制器的自定义过渡”演讲中对此进行了概述,该演讲还介绍了如何实现自己的过渡委托。


您可能会在iOS7中看到我针对上述问题的解决方案:https : //github.com/hightech/iOS-7-Custom-ModalViewController-Transitions


九州编程
浏览 775回答 3
3回答

红糖糍粑

iOS8以上在iOS8 +中,您现在可以使用新的modalPresentationStyle UIModalPresentationOverCurrentContext来呈现具有透明背景的视图控制器:MyModalViewController *modalViewController = [[MyModalViewController alloc] init];modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           [self presentViewController:modalViewController animated:YES completion:nil];    

素胚勾勒不出你

Swift 3和iOS10解决方案://create view controllerlet vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "RoadTripPreviewViewController")//remove black screen in backgroundvc.modalPresentationStyle = .overCurrentContext//add clear color backgroundvc.view.backgroundColor = UIColor.clear//present modalself.present(vc, animated: true, completion: nil)
打开App,查看更多内容
随时随地看视频慕课网APP