Neha Dangui
Neha Dangui

Reputation: 647

transparent background of the view presentViewController

I am using presentViewController to display a popup on click of a button in another 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

Upvotes: 2

Views: 3967

Answers (3)

Neha Dangui
Neha Dangui

Reputation: 647

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,

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

Upvotes: 0

BGreenstone
BGreenstone

Reputation: 360

I don't think there is a way to make this work 100% correctly with -presentViewController. You will probably have to use -addSubView which retains the transparency, and will render correctly. However, you'll have to make your own animation to cause it to animate in like -presentViewController does.

Upvotes: 0

MuhammadBassio
MuhammadBassio

Reputation: 1570

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.

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

Upvotes: 3

Related Questions