I usually seen UIControlState.normal
used, for example to set a titleColor
on a UIButton
object that will be used among all control states, as the UIButton. setTitleColor
docs says:
In general, if a property is not specified for a state, the default is to use the normal value. If the normal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
But I have also found UIControlState.init()
being used, for example following line:
UIButton.setTitleColor(UIColor, for: UIControlState())
Was wondering how it differs, thanks in advance!
UIControlState.normal
is a specific control state representing the "normal" state.
UIControlState()
is an "empty" control state with some undocumented default value.
It just so happens that both of these are backed with a raw value of 0
. So it seems that both represent the "normal" state.
But it is bad practice to rely on this. The defaults could change. It is much better to use the specific, clearly documented values provided.
Always use UIControlState.normal
for the "normal" state. Never use UIControlState()
since its value is undocumented and there is no guarantee it will always have the same underlying value as UIControlState.normal
.
Just found in console, both UIControlState()
and .normal
are equal, so there is no difference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With