25

The opaque property of a UIView is by default always set to "YES". But the UIView class reference states this:

An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable.

Since changing the alpha of a view is quite common, especially during transitions or animations, then the above statement would imply that you must always manually set opaque to NO if you are going to change the alpha property as well.

But I have never manually adjusted opaque and haven't had any noticeable symptoms. How necessary is it to make this consideration?

johnbakers
  • 24,158
  • 24
  • 130
  • 258

2 Answers2

17

The answer is that iOS is smart enough to recognize that if your view's alpha is less than 1, it needs to draw the content behind your view, regardless of your view's opaque property.

In response to comments: from my limited experimentation, I don't think the view's opaque property has any effect. (I think the documentation is wrong.) The view's layer's opaque property does have an effect: it controls whether the CGContext passed to drawRect: has an alpha channel. If the layer's opaque property is YES, the context has no alpha channel (and is treated as if every pixel has alpha of 1.0).

Changing the view's opaque property has no effect on the layer's opaque property. This is different than (for example) the view's alpha property, which is just a wrapper for the layer's opacity property.

In theory, having documented that the opaque property allows them to optimize drawing, Apple could implement that optimization in the future. In practice, doing so would probably break a lot of apps, so they probably won't make such a change apply to apps linked against older SDKs. (They have the ability to make UIKit behave differently depending on which version the app was linked with.)

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 3
    But this doesn't answer the question... as @andrewx said: If you ignore the docs and use transparent areas with `opacity=YES` everything seems to be fine. Besides the fact that the docs would be wrong then, the optimization based on the property's value described in the docs as well (not posted) seems not to take place at all. Or if it does, it's not properly described by the docs. Question to me is: Should I take care of the property and if so why? Performance? Could there be bad rendering if I don't? Any experiences anyone? Reproducible? – Kai Huppmann May 04 '12 at 09:58
  • 1
    I agree, this is still unresolved for me. if iOS is smart enough to ignore the opaque property, then what is the purpose of this property to begin with? – johnbakers May 04 '12 at 14:55
  • 2
    @rob mayoff Thank's for editing. I did some testing on this last hour and observed exactly what you describe. I have an app with a full screen scrollview containing smaller scrollviews as lines, each containing a lot of subviews, with a lot of subviews with transparent backgrounds and even drop shadows. We had some performance issues with scrolling, we fixed by deleting transparency and shadows. However, for my tests now I switched on/off transparency, shadows and the opaque property: In no configuration the change of opaque prop had any effect on proper rendering, performance or memory usage. – Kai Huppmann May 04 '12 at 19:45
  • From my point of view, your assumption, that Apple wanted to open the door for later optimizations by documenting unexisting functionality, sounds evident. Hopefully, hardware improvements will make that needless - otherwise I had a lot of bug-fixing to do. – Kai Huppmann May 04 '12 at 19:54
  • Could it be that, `UIKit` is smart enough to check a view's `opaque` and `alpha` to determine whether it is fully opaque, but what if deep down in a subview or grand-subview, there are some transparencies? I suspect that if you set `opaque` to YES and `alpha` to 1 for a parent view, then in some case, a subview's transparency may not work, because UIKit optimized the drawing. If the parent view has `opaque` set to NO, then there won't be such optimization. – nonopolarity Sep 01 '12 at 13:01
1

As long as the view contents itself (not its subviews) don't have alpha you're fine. So if you init an UIImageViews with an PNG image with alpha, opaque will be set to NO automatically.

Normally you don't really need many non-opaque views. But alpha of the complete view is smth different anyway.

calimarkus
  • 9,955
  • 2
  • 28
  • 48