How do opaque
alpha
and the opacity of the background work together for a UIView and what are the differences between them?

- 22,579
- 24
- 131
- 223
-
http://stackoverflow.com/questions/17079160/how-to-correctly-set-alpha-of-uiview-ios/17079360#17079360 – the1pawan Dec 20 '13 at 08:00
3 Answers
opaque
means don't draw anything underneath, even if you are transparent.
The background color's alpha only affects the background color's transparency, not anything else drawn on the view.
alpha
affects everything drawn on the view.
The opaque property can give you a speed increase - if you know that your view will never have transparency you can set this to YES
and when iOS renders your view it can make some performance optimisations and render it faster. If this is set to NO
iOS will have to blend your view with the view underneath, even if it doesn't happen to contain any transparency.
The alpha will also affect the alpha of the backround color i.e. if the background color is 0.5 transparent and the alpha is also 0.5, this has the effect of making the background view's alpha 0.25 (0.5 * 0.5).

- 113,588
- 46
- 195
- 237

- 38,189
- 13
- 98
- 110
-
2I think you mean background color's alpha is 0.25, not background view's. I suppose background view and background color are two different things. – Philip007 Dec 26 '12 at 06:37
-
I mean the alpha component of the `backgroundColor` property of the `UIView` we are talking about :) I've not talked about nested views at all! – deanWombourne Dec 29 '12 at 09:20
-
3This is set to YES by default. @property(nonatomic,getter=isOpaque) BOOL opaque; // default is YES. ... – Ryan Jun 17 '13 at 09:11
To the very good answer by deanWombourne it's worth to add that, unless you don't draw your own content using the drawRect: method, the opaque property has no effect.
You only need to set a value for the opaque property in subclasses of UIView that draw their own content using the drawRect: method. The opaque property has no effect in system-provided classes such as UIButton, UILabel, UITableViewCell, and so on.
If you draw your own content, keep in mind, that opaque is just a hint
This property provides a hint to the drawing system as to how it should treat the view.
and some more guidelines from the same Apple's doc:
If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.
iOS alpha vs opacity vs opaque
UIView.alpha
is equal to CALayer.opacity
- [0.0 - 1.0] - apply alpha
to all view(subviews, sublayers). It's like make a single flat bitmap image based on all content and apply alpha. That is why if view contains another subview or sublayer - no Blended Layers
and is Off-screen Rendered
is applied
UIView.backgroundColor
is equal to CALayer.backgroundColor
- applies color only on background(not subviews, sublayers)
UIView.opaque
is equal to CALayer.opaque
- Boolean property which hint's framework about some optimizations. But on practice it is not visible
Experiments
input


alpha
view1.alpha = 0.5
//or
view1.layer.opacity = 0.5
result and Off-screen Rendered


backgroundColor
view1.backgroundColor = .cyan.withAlphaComponent(0.5)
//or
view1.layer.backgroundColor = UIColor.cyan.withAlphaComponent(0.5).cgColor
result and Blended Layers



- 29,217
- 8
- 193
- 205