0

How to change the background color of a button when it is press?

@property (weak, nonatomic) IBOutlet UIButton *buttonDeleteOutlet;

My button has not the method: setBackgroundColor (UIColor *) forState:(UIControlState *). Only setBackgroundColor:(UIColor *).

No visible @interface for 'UIButton' declares the selector 'setBackgroundColor:forState:'

Why?

Bertrand Caron
  • 2,525
  • 2
  • 22
  • 49
Adam Shakhabov
  • 1,194
  • 2
  • 14
  • 35

3 Answers3

3

You can also try this

  (a) I think there is no method like setBackgroundColor: forState: in iOS 6 or iOS 7. So you have to put background color images using below methods

    [btn setBackgroundImage:[UIImage imageNamed:@"yourRedButton.png"] forState:UIControlStateHighlighted];
    [btn setBackgroundImage:[UIImage imageNamed:@"yourBlueButton.png"] forState:UIControlStateNormal];

 (b) Using Tint Color

    [YourButton setTintColor:[UIColor color]];
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
  • My button has not the method `setBackgroundColor (UIColor *) forState:(UIControlState *)`. Only `setBackgroundColor:(UIColor *)`. Why? – Adam Shakhabov Nov 20 '13 at 15:15
  • 1
    You have to set BackGround Image inplace of background color – Pradhyuman sinh Nov 21 '13 at 05:02
  • Here's an answer in Swift if someone stops by and is interested: http://stackoverflow.com/questions/26600980/how-do-i-set-uibutton-background-color-forstate-uicontrolstate-highlighted-in-s/30604658#30604658 – winterized Jun 02 '15 at 19:20
1

You need to set button's color for normal and highlighted state, such like

[yourButtonName setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal];
[yourButtonName setBackgroundColor:[UIColor blueColor] forState:UIControlStateHighlighted];
iPatel
  • 46,010
  • 16
  • 115
  • 137
0
[myButton setBackgroundColor:[UIColor blackColor] forState:UIControlStateNormal];

while it is clicked state:

[myButton setBackgroundColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[myButton setBackgroundColor:[UIColor blackColor] forState:UIControlStateSelected];
Albara
  • 1,306
  • 8
  • 14