Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between `UIControlState()` and `UIControlState.normal` when configuring an object

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!

like image 813
AamirR Avatar asked Aug 13 '17 16:08

AamirR


2 Answers

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.

like image 75
rmaddy Avatar answered Oct 24 '22 09:10

rmaddy


Just found in console, both UIControlState() and .normal are equal, so there is no difference

like image 2
AamirR Avatar answered Oct 24 '22 09:10

AamirR