Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController transparent background

Tags:

xcode

ios

I am creating a viewcontroller in this way:

 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"PhotoViewControlleriPhone" bundle:nil];
                                UIViewController *vc = [sb instantiateInitialViewController];

                               vc.view.backgroundColor = [UIColor clearColor];
                               self.modalPresentationStyle = UIModalPresentationCurrentContext;

                               [self presentModalViewController:vc animated:NO];

                               vc.view.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y + 64, imageView.frame.size.width, 200.000000);

                               vc.view.layer.cornerRadius = 10; // this value vary as per your desire
                               vc.view.clipsToBounds = YES;

The viewcontroller is not full screen, so you can still see the previous one. I want to be able to see it, but lock it. Just like when you use ios facebook sharing, you see the background, but it becomes darker and you can't interact with it. How can I do this?

like image 855
Alessandro Avatar asked Dec 26 '22 13:12

Alessandro


1 Answers

I believe the problem is that you’re displaying it using -presentModalViewController:animated:. Using that method carries with it some assumptions about the view controller you’re hosting; one of the assumptions it makes (on iPhone-type devices, at least) is that the view controller takes up the entire screen and is opaque.

To get around this, try adding the modal view controller’s view to the current view controller’s view manually. You’ll need to set the view controller hierarchy up to match the view hierarchy, so your code would look like this:

[self addChildViewController:vc];
[self.view addSubview:vc.view];

You’ll need to adjust the incoming view’s frame to position it within its new superview, but that should allow you more freedom.

like image 182
Jeff Kelley Avatar answered Jan 10 '23 16:01

Jeff Kelley