31

I have the following code in a viewController, all the outlets and action are hooked up correctly. the WHITE and PURPLE are UIColors that I've defined constants for. I've also set the UIWindow's tintColor to PURPLE and that propagates down to the button.

- (void)viewDidLoad {
    [super viewDidLoad];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    button.backgroundColor = WHITE;
    button.layer.borderWidth = 1.0;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 5.0;
    button.layer.borderColor = PURPLE.CGColor;
}

-(IBAction) buttonTouchDown:(id)sender {
    button.backgroundColor = PURPLE;
    button.layer.borderColor = WHITE.CGColor;
}

-(IBAction) buttonTouchUpOutside:(id)sender {
    button.backgroundColor = WHITE;
    button.layer.borderColor = PURPLE.CGColor;
}

-(IBAction) buttonTouchUpInside:(id)sender {
    button.backgroundColor = WHITE;
    button.layer.borderColor = PURPLE.CGColor;
}

When I click the button the text doesn't go white like i told it to in viewDidLoad

here's some screenshots that I could've cropped better! As you can see in the highlighted state it's not white but like a white and purple mix. Will I need to use UIButtonTypeCustom? I heard if I do that I won't get the advantages of iOS 7 doing its magic with tintColor. Not sure what's the correct way to go about this. Thanks in advance.

normalState highlightedState

Adam Langsner
  • 1,276
  • 1
  • 15
  • 23

2 Answers2

60

Shouldn't you be using UIControlStateSelected?

Okay, I just tried this out myself it don't want to work well.

You need to set your button style to Custom. The UIButton system style does a lot of things that you can't change. The highlighted button state defaults to its own implementation which is a lighter version if your tintcolor.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

That should get you the white text background.

David Wong
  • 10,284
  • 3
  • 39
  • 34
  • You shouldn't need to use `UIControlStateSelected` because `UIButton` does not have that state. `UIControlStateSelected` is for things like the selected tab of a `UISegmentedControl`. – greenisus Mar 06 '14 at 19:04
  • 5
    @greenisus please note that UIButton, UISegmentedControl and other UIKit classes inherit off UIControl which has selected state. Both Interface Builder and code support the visual changes made using UIControlStateSelected UIControlStateDisabled, etc. Great thing about UIControl's selected property is calling isSelected from the button and allowing the button to handle the boolean property. – David Wong Mar 06 '14 at 23:45
  • 3
    Setting button style to Custom worked for me! Thanks! – AXE Mar 10 '14 at 11:46
-1

Hope this is the answer of your question or someone still finding difficulty to set title color when button selected.

[btnCustom setTitleColor:[UIColor whiteColor] forState: UIControlStateSelected];
[btnCustom setTitleColor:[UIColor redColor] forState: UIControlStateNormal];

when you want button title coloe red than make btnCustom.selected = NO and if you want button with white title color than make btnCustom.selected = YES

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
Seema Sharma
  • 379
  • 3
  • 12