为了账号安全,请及时绑定邮箱和手机立即绑定

在UIViewController上显示clearColor UIViewController

在UIViewController上显示clearColor UIViewController

九州编程 2019-10-05 11:29:25
我UIViewController在另一个UIViewController视图之上有一个视图作为子视图/模态,例如,子视图/模态应该是透明的,添加到子视图中的任何组件都应该是可见的。问题是我所拥有的是子视图显示黑色背景而不是clearColor。我试图将其UIView作为clearColor而不是黑色背景。有人知道这是怎么回事吗?任何建议表示赞赏。FirstViewController.mUIStoryboard *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.mUIStoryboard *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
查看完整描述

3 回答

?
红糖糍粑

TA贡献1815条经验 获得超6个赞

iOS8以上


在iOS8 +中,您现在可以使用新的modalPresentationStyle UIModalPresentationOverCurrentContext来呈现具有透明背景的视图控制器:


MyModalViewController *modalViewController = [[MyModalViewController alloc] init];

modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           

[self presentViewController:modalViewController animated:YES completion:nil];    


查看完整回答
反对 回复 2019-10-05
?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

Swift 3和iOS10解决方案:


//create view controller

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "RoadTripPreviewViewController")

//remove black screen in background

vc.modalPresentationStyle = .overCurrentContext

//add clear color background

vc.view.backgroundColor = UIColor.clear

//present modal

self.present(vc, animated: true, completion: nil)


查看完整回答
反对 回复 2019-10-05
  • 3 回答
  • 0 关注
  • 769 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信