简体   繁体   English

视图presentViewController的透明背景

[英]transparent background of the view presentViewController

I am using presentViewController to display a popup on click of a button in another ViewController. 我正在使用presentViewController在另一个ViewController中单击按钮时显示弹出窗口。 But I am getting a black background. 但是我的背景是黑色的。 I need a transparent background. 我需要透明的背景。 I tried the following, 我尝试了以下方法

UIView v1 = [[UIView alloc] init];
v1.alpha = 0.0f;
v1.backgroundColor = [UIColor clearColor];
[self.view addSubview:v1];
UIView v2 = [[UIView alloc] initWithFrame:10,10,270,270];
v2.backgroundColor = [UIColor whiteColor];
[v1 addSubView:v2];

This displays the popup, but blanks out the screen later. 这将显示弹出窗口,但稍后将其空白。 Can anyone please help me. 谁能帮帮我吗。 Thanks in advance. 提前致谢。

Regards, 问候,

Neha NEHA

If you are testing on an iPhone, then presentViewController hides the parent view once the presented view is presented, so your request isn't valid. 如果您正在iPhone上进行测试,则一旦呈现了呈现的视图, presentViewController隐藏父视图,因此您的请求无效。

If you are on an iPad, then presentViewController can have the effect you want by setting the modalPresentationStyle property of the presented viewcontroller to UIModalPresentationFormSheet 如果你是在iPad上,然后presentViewController可以通过设置你想要的效果modalPresentationStyle呈现视图-控制的财产UIModalPresentationFormSheet

I found the following solution which works perfectly for me. 我找到了下面的解决方案,非常适合我。

In the first ViewController.m where I am using the presentViewController I added the below code, 在我使用presentViewController的第一个ViewController.m中,添加了以下代码,

SecondViewController *second = [[SecondViewController alloc] init];
second.view.backgroundColor = [UIColor clearColor];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:second animated:YES completion:nil]

I don't think there is a way to make this work 100% correctly with -presentViewController. 我不认为有一种方法可以通过-presentViewController正确地100%完成这项工作。 You will probably have to use -addSubView which retains the transparency, and will render correctly. 您可能必须使用-addSubView,它可以保留透明度并可以正确呈现。 However, you'll have to make your own animation to cause it to animate in like -presentViewController does. 但是,您必须制作自己的动画以使其像-presentViewController一样进行动画处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM