UIButton Background Color

It seems to be possible to change the background color of the system UIButton, but it remains white in the focused state. I can switch the button to custom but I then lose the shadows etc. for the focused state, is there a way to reenable them with a custom button. Or does it mean if I want to change the color of a button, you need to roll your own focused state, and lose out on the system provided one? I would like to keep that experience as consistent as possible with the system.

Answered by andrewconlan in 60866022

Solved this by keeping the button set to system, and adding background images (using slicing) for the various states in the color that I wanted. This keeps the inbuild system focus

Override didUpdateFocusInContext method and check if next focus view is button, if yes then customize its UI, and to set it back to orignal state check context.previousFocusedView was that button, something like below

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator

{

if (context.nextFocusedView == _button)

{

_synopsisText.backgroundColor = [UIColor whiteColor];

[_synopsisText setTextColor:[UIColor blackColor]];

}


}

Thanks for the reply, but the button still remains white in the focused state, at least in the simulator anyway, if I try and change

the background color to something else in the didUpdateFocus call

Actually this seems to be more or less the same discussion, looks like at the moment, we need to roll our own focused state


https://forums.developer.apple.com/message/54050

Accepted Answer

Solved this by keeping the button set to system, and adding background images (using slicing) for the various states in the color that I wanted. This keeps the inbuild system focus

Can you elaborate on how you accomplished this? When I set the backgroundImage property, the titleLabel becomes obscured. I believe this is a bug. Did you use a backgroundImage in combination with the titleLabel? If so, how did you get it to show up?

Thanks!

Just tried dragging a UIButton into the storyboard (xcode 7.1.1) and setting the background image in the storyboard, the label does disappear but then if I build and run in the simulator the label is visible. Looks like a bug with interface builder. If in the storyboard you then set an image aswell and then remove it the label then appears in the storyboard, looks like it's radar time.

UIButton Background Color
 
 
Q