22

I have a question about UIView, what's the difference between views hidden, alpha and opaque?

The effect of setting view: hidden = yes and view.alpha = 0.0f is the same.

heckman
  • 499
  • 3
  • 8
joebo
  • 235
  • 1
  • 3
  • 6

2 Answers2

43

The differences are subtle. According to the UIView class reference:

  • opaque tells the system that the view has no transparency and is thus faster to render because calculations for blending can be skipped
  • hidden is boolean property that changes only the visibility of the current view and hides it from ui events.
  • alpha is an animatable property

Setting alpha = 0.0f or hidden = YES has the same visual effect. However using hidden to actually hide a view not only in a graphical sense but also from ui events might result in a more efficient responder chain when you have lots of nested views.

Torsten Walter
  • 5,614
  • 23
  • 26
  • 18
    +1 Note that UIKit does actually treat very-low alpha elements as hidden for the purposes of UI events. The last time I experimented with it, the threshold was 0.1, but this isn't documented and the specific threshold shouldn't be relied on. But if you animate alpha to 0, it isn't generally necessary to then hide it too. – Rob Napier Jun 07 '12 at 02:49
  • In case you're wondering like me, if they are somehow connected, the answer is no. If you set the `hidden` property to `YES`, then changing the `alpha` won't have any visual effect. – streem Jan 20 '15 at 10:04
  • It doesn't seem to be the case that the actual `hidden` property is set to `YES` when the alpha of a property gets set to 0.0f - you can check with the debugger for yourself – Benzy Apr 28 '15 at 12:37
  • 1
    As an addendum, hiding a view in a UIStackView's arranged subviews does not have the same effect as setting the alpha as 0 – cyril Sep 19 '18 at 05:35
  • I believe that the view with alpha = 0 will still receive gesture events, such as tap or drag. – Borzh Oct 21 '19 at 18:55
-3

setting view.hidden = yes will hide the view and view.alpha = 0.0f will set the colors of view alpha 0.0 which will make the view invisible, so both are not same.... :)

Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39
  • 3
    by setting view.alpha = 0.0f, iOS also sets view.hidden = yes internally, in fact, there is a threshold for alpha (0.1f), for alpha values smaller than this threshold, view.hidden gets set to YES. – Scholle Jan 12 '13 at 11:24