DevDevDev DevDevDev - 1 year ago 128
Objective-C Question

100% opacity UILabel over a 50% opacity background (UIView?)

So right now I have a

with a
in it. I want the background to have an opacity < 1.0 and the label to have an opacity of 1.0. However since alphas propagate down the view hierarchy, the label ends up with an opacity < 1.0 as well.

Is there anyway to do what I want without making the
a subview of another view??

Answer Source

Just set the background color to be semitransparent:

view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];

Or, in Swift:

view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)

Or, Swift 3:

view.backgroundColor =

Note that, in this particular case, UIColor(white: 0, alpha: 0.5) is more concise, but colorWithAlphaComponent will work in general.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download