Denis Kharitonov
Denis Kharitonov

Reputation: 209

UIView background color with alpha in xib

I have a problem. I am trying to make my custom UIView with an image in the very center. The part outside of that image got to be transparent and grayed. In xib for "View" I've set this:My View's settings in xib

The result - fail. The region outside the image is just gray, not transparent. But if I add

-(void) viewDidLoad
{
  self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
}

Then everything works as expected. What was I doing wrong when trying to achieve transparent backround in xib?

UPDATE: I found the code which was doing presenting of the view, and there was alpha set inside of it. So I guess that was the problem. Thanks. Sorry for being so newbie

Upvotes: 1

Views: 3388

Answers (3)

Denis Kharitonov
Denis Kharitonov

Reputation: 209

The issue was in custom show method. It didn't use presentViewController:animated:completion: method. It used alpha animaiton from 0 to 1. The second - when using standard presentViewController:animated:completion: method cocoa optimization was hiding the underlying view, so I need to set up (for iOS 8)

vc_to_present_on.modalPresentationStyle = UIModalPresentationCurrentContext;
vc_to_be_presented.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

And then everything work as expected

Upvotes: 1

Zigii Wong
Zigii Wong

Reputation: 7826

Maybe this one is what you expected. You can only change the background Color rather the view's alpha. Just set opacity with the background Color.

enter image description here

Upvotes: 6

Pheepster
Pheepster

Reputation: 6357

When setting the alpha of a parent UIView, the alpha value will be passed to the child views, giving the same alpha to all the child views. If you set the alpha value of the UIView to 1, you are setting the alpha of the view, not the color.

That said, you can set the alpha of the background color. When you do this, the view maintains its own alpha value, while the background color has its own alpha value. Hope this helps

Upvotes: 2

Related Questions