1

I'm using Xcode 6.1.1

Is it possible to set the background colour in code for the selected/highlighted state for UIButton? - Not a background image!

I'm using ID's for colors (e.g. @"theme.brand.primary") that are linked to a json file i.e. .plist file

I've done it for setTitleColor UIControlStateHighlighted which works fine... example code:

[button setTitleColor:[config getColour:@"button.text.pressed-state"] forState:UIControlStateHighlighted];

However I want the colour of the button to change not the title.

There is no option for setBackgroundColor:forState: ???

Does anyone know if theres a simple way to do this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
lyslop
  • 11
  • 1
  • Not directly possible according to the docs. You'll need to set a `UIImage` (of a specific colour) for each state using `setBackgroundImage:forState:`. – Robotic Cat Mar 24 '15 at 15:42
  • You can subclass UIButton and override setHighlighted method. You can follow this post http://stackoverflow.com/questions/20300766/how-to-change-the-highlighted-color-of-a-uibutton – cyt Mar 24 '15 at 15:42

2 Answers2

0

Can you not just check for highlighted?

if (myButton.highlighted) {        
    myButton setBackgroundColor.......
}
pidge
  • 20
  • 3
0

I have open-sourced a class, STAButton, to fill this functionality hole. Available under the MIT license. Works for iOS 7+ (I have not tested with older iOS versions).

Stunner
  • 12,025
  • 12
  • 86
  • 145