Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background color of a UIButton while it's highlighted?

At some point in my app I have a highlighted UIButton (for example when a user has his finger on the button) and I need to change the background color while the button is highlighted (so while the finger of the user is still on the button).

I tried the following:

_button.backgroundColor = [UIColor redColor]; 

But it is not working. The color remains the same. I tried the same piece of code when the button is not highlighted and it works fine. I also tried calling -setNeedsDisplay after changing the color, it didn't have any effect.

How to force the button to change the background color?

like image 988
MartinMoizard Avatar asked Jan 25 '13 14:01

MartinMoizard


People also ask

How can I change Uibutton color in Swift?

Add a button on your storyboard, select it Go to it's attribute inspector and select 'Background' property to choose the color.

How do you change Backcolor buttons?

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.


2 Answers

You can override UIButton's setHighlighted method.

Objective-C

- (void)setHighlighted:(BOOL)highlighted {     [super setHighlighted:highlighted];      if (highlighted) {         self.backgroundColor = UIColorFromRGB(0x387038);     } else {         self.backgroundColor = UIColorFromRGB(0x5bb75b);     } } 

Swift 3.0 and Swift 4.1

override open var isHighlighted: Bool {     didSet {         backgroundColor = isHighlighted ? UIColor.black : UIColor.white     } } 
like image 55
Thomas Decaux Avatar answered Oct 06 '22 09:10

Thomas Decaux


Not sure if this sort of solves what you're after, or fits with your general development landscape but the first thing I would try would be to change the background colour of the button on the touchDown event.

Option 1:

You would need two events to be capture, UIControlEventTouchDown would be for when the user presses the button. UIControlEventTouchUpInside and UIControlEventTouchUpOutside will be for when they release the button to return it to the normal state

UIButton *myButton =  [UIButton buttonWithType:UIButtonTypeCustom]; [myButton setFrame:CGRectMake(10.0f, 10.0f, 100.0f, 20.f)]; [myButton setBackgroundColor:[UIColor blueColor]]; [myButton setTitle:@"click me:" forState:UIControlStateNormal]; [myButton setTitle:@"changed" forState:UIControlStateHighlighted]; [myButton addTarget:self action:@selector(buttonHighlight:) forControlEvents:UIControlEventTouchDown]; [myButton addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlEventTouchUpInside]; 

Option 2:

Return an image made from the highlight colour you want. This could also be a category.

+ (UIImage *)imageWithColor:(UIColor *)color {    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);    UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();     CGContextSetFillColorWithColor(context, [color CGColor]);    CGContextFillRect(context, rect);     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();     return image; } 

and then change the highlighted state of the button:

[myButton setBackgroundImage:[self imageWithColor:[UIColor greenColor]] forState:UIControlStateHighlighted]; 
like image 29
Tim Avatar answered Oct 06 '22 08:10

Tim