iosobjective-cuibuttonuicontrolstate

UIButton Multiple Labels Using UIControlStates


I know with a UIButton, I can add additional UILabels as subviews:

[myButton addSubview: myLabel];

And (at least, with the default title label) I can set its text color when tapped by using:

[myButton setTitleColor:someColor forState:UIControlStateHighlighted]

My question is, how can I implement this functionality for additional UILabels added to the UIButton (if this is possible)?


Solution

  • Subclass UIButton and add your additional labels in there as instance variables. Then override -setHighlighted and -setSelected to adjust the additional labels as desired. FYI - you call [myButton setTitleColor...], not [myButton.titleLabel setTitleColor...]