0

How to load UIViewController with transparent Background color? So that previous view controller can be visible. I have tried creating custom view with below method but it does not work.

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    {
        CGColorRef colorRef=[UIColor clearColor].CGColor;
        CGContextSetFillColorWithColor(ctx, colorRef);
//        CGColorRelease(colorRef);
        CGContextFillRect(ctx, rect);
    }
    CGContextRestoreGState(ctx);
}

Any help will be appreciable!

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56

2 Answers2

1

You can do that by adding a subview.

//Get your screen size
CGRect screenBounds = [[UIScreen mainScreen] bounds];
//Create the transparent view of the same size
UIView* transparentView = [[UIView alloc] initWithFrame:screenBounds];
// change the background color to black and the opacity to 0.5 as this will make it look transparent
transparentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
// add this new view to your main view
[self.view addSubview:transparentView];

Then in this view you can add all of your UI Elements. This will make it look like a transparent glass is put over the old view.

When you're finished, you just remove from super view.

[transprentView removeFromSuperview];

EDIT:

Or you could do it like this answer.

Hope that helps, Julian

Community
  • 1
  • 1
Julian E.
  • 4,687
  • 6
  • 32
  • 49
1

To make your view controller's view transparent viewController.view.backgroundColor = [UIColor clearColor];

But, it won't help you to see content of view controller behind it in all situations. For example, if your controllers are in navigation controller - it wouldn't. In that case - before pushing new controller on top, make snapshot view with method and use it as an layer under all content of top controller.

Like

 UIView *backgroundView = [navVC.topViewController.view snapshotViewAfterScreenUpdates:NO];

 [nextVC.view insertSubview:backgroundView atIndex:0];
 [navVC psuhViewController:nextVC animated:YES];
Sergii Martynenko Jr
  • 1,407
  • 1
  • 9
  • 18