what's the difference between view's hidden = yes and alpha = 0.0f
Asked Answered
D

2

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.

Duodiode answered 6/6, 2012 at 9:40 Comment(0)
R
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.

Rumilly answered 6/6, 2012 at 9:56 Comment(5)
+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.Dumpcart
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.Zitazitah
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 yourselfHuppert
As an addendum, hiding a view in a UIStackView's arranged subviews does not have the same effect as setting the alpha as 0Toneme
I believe that the view with alpha = 0 will still receive gesture events, such as tap or drag.Macomber
B
-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.... :)

Brahmin answered 6/6, 2012 at 9:45 Comment(1)
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.Overmatch

© 2022 - 2024 — McMap. All rights reserved.