Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa/iPhone: BackgroundColor and Opaque Properties

In Cocoa, specifically the iPhone SDK, the opaque property is described as:

If opaque, the drawing operation assumes that the view fills its bounds and can draw more efficiently. The results are unpredictable if opaque and the view doesn’t fill its bounds. Set this property to NO if the view is fully or partially transparent.

In my experience, if you have a view (label, table cell, etc.) with backgroundColor set to [UIColor clearColor], you do not need to set opaque to NO for it to appear properly (with a clear background).

Intuitively, doing this would require also setting opaque to NO, but I've never run into problems.

Can you mix opaque=YES and clearColor, or am I living on borrowed time? It doesn't seem to be specifically documented anywhere.

like image 257
Adam Ernst Avatar asked Feb 07 '09 23:02

Adam Ernst


1 Answers

Try it and see is the only way forward on the iPhone, because like you say, despite the volume of the documentation that ships with the SDK, it's not very specific in many cases.

As for opaque though, this is just a hint to the compositing engine that tells it it doesn't need to bother to displaying any layers that are covered by the opaque layer. However, the compositing is done by the graphics chip on the phone, so in many cases it is not more efficient to not draw the obscured part of a partially obscured layer, which is most likely why you are not seeing things get messed up at the moment (i.e. cocoa is ignoring the setting in the cases you've tried). By the same token you are not seeing a performance improvement from setting opaque to true.

So my advice would be to stick with using the opaque property the way the docs say because you are risking a buggy rendering for no real benefit.

like image 99
U62 Avatar answered Oct 13 '22 00:10

U62