By MartinMoizard


2013-01-25 14:04:22 8 Comments

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?

26 comments

@Jake Hall 2014-10-07 17:54:25

In Swift you can override the accessor of the highlighted (or selected) property rather than overriding the setHighlighted method

override var highlighted: Bool {
        get {
            return super.highlighted
        }
        set {
            if newValue {
                backgroundColor = UIColor.blackColor()
            }
            else {
                backgroundColor = UIColor.whiteColor()
            }
            super.highlighted = newValue
        }
    }

@shimizu 2014-11-13 07:20:48

This totally works, but I'm confused how you were able to figure this out? The parameters aren't in the documentation or UIButton.h as far as I can tell.

@Jake Hall 2014-11-13 15:25:30

This is the swift syntax which emulates the behavior of overriding setHightlighted in objective c. See documentation on Computed Properties here developer.apple.com/library/ios/documentation/Swift/Conceptu‌​al/…

@ChikabuZ 2015-03-13 13:06:35

thank you, also work for "enabled" state

@Dam 2015-03-17 17:57:17

In swift you could use didSet

@Aleksejs Mjaliks 2015-03-21 18:28:48

I have added example with property observer: stackoverflow.com/a/29186375/195173.

@Adam Johns 2015-05-29 03:58:36

I think what @shimizu was asking was how did you know highlighted was a property on UIButton. The answer is that it is a property on UIControl which UIButton inherits from.

@Aleksejs Mjaliks 2015-03-21 18:24:07

There is no need to override highlighted as computed property. You can use property observer to trigger background color change:

override var highlighted: Bool {
    didSet {
        backgroundColor = highlighted ? UIColor.lightGrayColor() : UIColor.whiteColor()
    }
}

Swift 4

override open var isHighlighted: Bool {
    didSet {
        backgroundColor = isHighlighted ? UIColor.lightGray : UIColor.white
    }
}

@bloudermilk 2015-10-01 14:24:38

This is the proper Swift answer

@Dave G 2016-04-22 09:33:46

I've never used functionality like this. Can you explain where this goes? Is it in the IBAction buttonPress function or in the viewDidLoad?

@Slavcho 2016-08-11 13:15:06

What if I have multiple UIButtons with different colors?

@James Toomey 2017-05-17 23:44:35

@Dave G, you create a new subclass of UIButton by clicking File>New>File>Cocoa Touch Class and setting it to subclass of UIButton. Name the file for ex CustomButton, which will become both the file name and the class name. Inside this file, put the override var highlighted code shown above. Last step, set the UIButton on Interface Builder to use this CustomButton subclass by going to the Property page where it says "Custom Class" and has a dropdown box. It will say "UIButton" in grey letters. The dropdown list should show CustomButton. Select this, and the button is now subclassed.

@Daniel Springer 2018-12-23 01:51:45

What black magic is this?

@Federico Zanetello 2016-03-26 03:10:16

a more compact solution (based on @aleksejs-mjaliks answer):

Swift 3/4+:

override var isHighlighted: Bool {
    didSet {
        backgroundColor = isHighlighted ? .lightGray : .white
    }
}

Swift 2:

override var highlighted: Bool {
    didSet {
        backgroundColor = highlighted ? UIColor.lightGrayColor() : UIColor.whiteColor()
    }
}

If you don't want to override, this is an updated version of @timur-bernikowich's answer (Swift 4.2):

extension UIButton {
  func setBackgroundColor(_ color: UIColor, forState controlState: UIControl.State) {
    let colorImage = UIGraphicsImageRenderer(size: CGSize(width: 1, height: 1)).image { _ in
      color.setFill()
      UIBezierPath(rect: CGRect(x: 0, y: 0, width: 1, height: 1)).fill()
    }
    setBackgroundImage(colorImage, for: controlState)
  }
}

@MacTeo 2016-08-26 13:22:44

Really nice solution. works like a charm

@Jeremiah 2017-05-22 16:24:06

This is awesome! Much cleaner than other options.

@Usama bin Attique 2017-11-28 08:24:13

@FedericoZanetello this will override isHighlighted in all the buttons in your app, which isn't a good solution in my opinion. ill go with Timur's answer.

@Federico Zanetello 2018-10-15 06:52:09

@UsamabinAttique I've updated my answer :)

@swordray 2018-09-03 18:09:29

Use https://github.com/swordray/UIButtonSetBackgroundColorForState

Add to Podfile using CocoaPods

pod "UIButtonSetBackgroundColorForState"

Swift

button.setBackgroundColor(.red, forState: .highlighted)

Objective-C

[button setBackgroundColor:[UIColor redColor] forState:UIControlStateHighlighted];

@Thomas Decaux 2013-07-11 19:56:34

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
    }
}

@Paul Brewczynski 2014-03-22 05:32:38

Yeah. It is kind of nice way to do this. Because you can define several similar buttons.

@Van Du Tran 2014-05-22 20:11:38

I did the same for setSelected: but the background does not change color..

@Stuart P. 2014-10-02 16:22:14

Someone mark this as the proper answer (and quit being afraid to subclass, it'll save you a lot of redundancy!)

@Beanno1116 2015-09-01 06:49:15

Just a newbie question, where would you subclass that button method? If I have a button in a view controller named ConversionViewController, how would I setup the button to change the background color when highlighted or tapped? Would I subclass the setHIghlighted in the COnversionViewController?

@HaloZero 2015-09-15 22:51:20

So there is one problem with this answer that I found. If you want to have the same color be available when you select theButton then setHighlighted gets called after setSelected and thus will override any selected styling you have. The solution above might be better if you want to select the button as well

@Yakiv Kovalsky 2016-11-09 10:30:41

Here is the problem - what if you need a few buttons of different color? Say, 1st is green when highlighted, 2nd is red and so on. You'll need to create a new class for each case.

@SK. 2016-12-04 18:55:24

@YakivKovalskiy assuming you're using a sub-class, you could add two UIColor properties e.g. normalBackground and highlightedBackground, then assign self.backgroundColor = normalBackground or highlightedBackground accordingly. Don't forget to add an init method for ease of use e.g. initWithBackground:highlightedBackground:

@n_b 2017-04-05 15:44:00

To expand on @SK. 's addition, these properties could also be made IBInspectable so that you could do all the init through IB.

@Jin Wang 2017-08-14 07:29:09

Should it be willSet instead of didSet?

@Fantini 2017-08-18 20:04:59

Nice solution, just one suggestion: backgroundColor = isHighlighted ? .lightGray : .white

@Fogh 2018-08-16 12:34:03

Works for isSelected as well

@evya 2018-06-28 10:13:48

class CustomButton: UIButton {

    override var isHighlighted: Bool {
        didSet {
            if (isHighlighted) {
                alpha = 0.5
            }
            else {
                alpha = 1
            }            
        }
    }

}

@manoj kumar 2017-02-15 06:10:29

Below UIIImage extension will generates image object with specified color parameter.

extension UIImage {
    static func imageWithColor(tintColor: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        tintColor.setFill()
        UIRectFill(rect)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
       }
    }

An example usage for a button can be applied for the button object as :

setupButton.setBackgroundImage(UIImage.imageWithColor(tintColor: UIColor(displayP3Red: 232/255, green: 130/255, blue: 121/255, alpha: 1.0)), for: UIControlState.highlighted)

setupButton.setBackgroundImage(UIImage.imageWithColor(tintColor: UIColor(displayP3Red: 255/255, green: 194/255, blue: 190/255, alpha: 1.0)), for: UIControlState.normal)

@Maverick 2016-10-05 08:22:23

UIButton extension with Swift 3+ syntax:

extension UIButton {
    func setBackgroundColor(color: UIColor, forState: UIControlState) {
        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        UIGraphicsGetCurrentContext()!.setFillColor(color.cgColor)
        UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
        let colorImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.setBackgroundImage(colorImage, for: forState)
    }}

Use it like:

YourButton.setBackgroundColor(color: UIColor.white, forState: .highlighted)

Original Answer: https://stackoverflow.com/a/30604658/3659227

@Giordano Scalzo 2014-11-23 22:55:16

An handy generic extension in Swift:

extension UIButton {
    private func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }

    func setBackgroundColor(color: UIColor, forUIControlState state: UIControlState) {
        self.setBackgroundImage(imageWithColor(color), forState: state)
    }
}

Swift 3.0

extension UIButton {
    private func imageWithColor(color: UIColor) -> UIImage? {
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        context?.setFillColor(color.cgColor)
        context?.fill(rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }

    func setBackgroundColor(_ color: UIColor, for state: UIControlState) {
        self.setBackgroundImage(imageWithColor(color: color), for: state)
    }
}

@Thomas Wana 2016-03-16 10:13:39

Spot on. This is it.

@Geoherna 2016-04-21 15:07:59

Excellent answer.

@Timur Bernikovich 2017-06-02 09:49:14

My best solution for Swift 3+ without subclassing.

extension UIButton {
  func setBackgroundColor(_ color: UIColor, for state: UIControlState) {
    let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
    UIGraphicsBeginImageContext(rect.size)
    color.setFill()
    UIRectFill(rect)
    let colorImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    setBackgroundImage(colorImage, for: state)
  }
}

With this extension it's easy to manage colors for different states and it will fade your normal color automatically in case highlighted color is not provided.

button.setBackgroundColor(.red, for: .normal)

@Elita 2017-04-25 12:14:53

Swift 3:

extension UIButton {
    private func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRect(x:0.0,y:0.0,width: 1.0,height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        context!.setFillColor(color.cgColor)
        context!.fill(rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image!
    }

    func setBackgroundColor(color: UIColor, forUIControlState state: UIControlState) {
        self.setBackgroundImage(imageWithColor(color: color), for: state)
    }
}

@Tim 2013-01-25 15:40:36

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];

@MartinMoizard 2013-01-30 11:10:17

The option 2 fits my needs, thanks!

@Evgen Bodunov 2014-01-22 09:03:09

Add UIControlEventTouchUpOutside and UIControlEventTouchCancel to buttonHighlight: event list and it will work always.

@Jack Solomon 2014-04-11 00:27:40

Option two is the best that I have found so far. I guess, however, that storyboards do have their advantages in this case!

@Van Du Tran 2014-05-22 20:17:40

Answer by Thomas is better and that's what I use also

@Sky 2014-07-24 18:44:53

If you're using layer.cornerRadius and go with option #2, you'll need to make sure to set clipsToBounds to true to get the image's corners rounded as well.

@winterized 2015-06-02 19:21:18

If someone stops by and needs an answer in Swift: stackoverflow.com/questions/26600980/…

@felixwcf 2016-05-26 15:24:27

Put the imageWithColor method in singleton, and use it so many times in many projects. Big help. Thank you so much.

@mxcl 2017-02-23 22:23:03

Here's a simple CocoaPod that makes images with colors, and also allows you to make resizable images with a corner radius and color: github.com/mxcl/UIImageWithColor

@Valentin Mercier 2018-04-19 13:01:52

If you do not fell like writing a new method just to generate an image you can also use [CIImage imageWithColor:]

@shoe 2017-02-01 09:00:05

Drop it in and you're good to go:
*proerty can be set in IB, and if no highlighted background is set, background will not change when pressed

private var highlightedBackgroundColors = [UIButton:UIColor]()
private var unhighlightedBackgroundColors = [UIButton:UIColor]()
extension UIButton {

    @IBInspectable var highlightedBackgroundColor: UIColor? {
        get {
            return highlightedBackgroundColors[self]
        }

        set {
            highlightedBackgroundColors[self] = newValue
        }
    }

    override open var backgroundColor: UIColor? {
        get {
            return super.backgroundColor
        }

        set {
            unhighlightedBackgroundColors[self] = newValue
            super.backgroundColor = newValue
        }
    }

    override open var isHighlighted: Bool {
        get {
            return super.isHighlighted
        }

        set {
            if highlightedBackgroundColor != nil {
                super.backgroundColor = newValue ? highlightedBackgroundColor : unhighlightedBackgroundColors[self]
            }
            super.isHighlighted = newValue
        }
    }
}

@Roman Bambura 2016-12-28 10:35:03

if you won't override just set two action touchDown touchUpInside

@Bence Pattogato 2016-11-24 09:02:25

Subclass the UIButton and add inspectable properties for convenient use (written in Swift 3.0):

final class SelectableBackgroundButton: UIButton {

    private struct Constants {
        static let animationDuration: NSTimeInterval = 0.1
    }

    @IBInspectable
    var animatedColorChange: Bool = true

    @IBInspectable
    var selectedBgColor: UIColor = UIColor.blackColor().colorWithAlphaComponent(0.2)

    @IBInspectable
    var normalBgColor: UIColor = UIColor.clearColor()

    override var selected: Bool {
        didSet {
            if animatedColorChange {
                UIView.animateWithDuration(Constants.animationDuration) {
                    self.backgroundColor = self.selected ? self.selectedBgColor : self.normalBgColor
                }
            } else {
                self.backgroundColor = selected ? selectedBgColor : normalBgColor
            }
        }
    }

    override var highlighted: Bool {
        didSet {
            if animatedColorChange {
                UIView.animateWithDuration(Constants.animationDuration) {
                    self.backgroundColor = self.highlighted ? self.selectedBgColor : self.normalBgColor
                }
            } else {
                self.backgroundColor = highlighted ? selectedBgColor : normalBgColor
            }
        }
    }
}

@Gabriel.Massana 2016-02-09 21:25:11

To solve this problem I created a Category to handle backgroundColor States with UIButtons:
ButtonBackgroundColor-iOS

You can install the category as a pod.

Easy to use with Objective-C

@property (nonatomic, strong) UIButton *myButton;

...

[self.myButton bbc_backgroundColorNormal:[UIColor redColor]
                 backgroundColorSelected:[UIColor blueColor]];

Even more easy to use with Swift:

import ButtonBackgroundColor

...

let myButton:UIButton = UIButton(type:.Custom)

myButton.bbc_backgroundColorNormal(UIColor.redColor(), backgroundColorSelected: UIColor.blueColor())

I recommend you import the pod with:

platform :ios, '8.0'
use_frameworks!

pod 'ButtonBackgroundColor', '~> 1.0'

Using use_frameworks! in your Podfile makes easier to use your pods with Swift and objective-C.

IMPORTANT

I also wrote a Blog Post with more information.

@Mike Zriel 2016-01-06 07:56:51

Here is the code in Swift to select for button state:

func imageWithColor(color:UIColor) -> UIImage {
    let rect:CGRect = CGRectMake(0.0, 0.0, 1.0, 1.0)
     UIGraphicsBeginImageContext(rect.size)
    let context:CGContextRef = UIGraphicsGetCurrentContext()!
    CGContextSetFillColorWithColor(context, color.CGColor)
    CGContextFillRect(context, rect)
    let image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
    return image;
}

Example:

    self.button.setImage(self.imageWithColor(UIColor.blackColor()), forState: .Highlighted)

@Karan Alangat 2015-03-27 11:53:53

Try this !!!!

For TouchedDown Event set One color and for TouchUpInside set the other.

- (IBAction)touchedDown:(id)sender {
    NSLog(@"Touched Down");
    btn1.backgroundColor=[UIColor redColor];
}

- (IBAction)touchUpInside:(id)sender {
    NSLog(@"TouchUpInside");
    btn1.backgroundColor=[UIColor whiteColor];    
}

@SudoPlz 2016-02-03 21:19:16

Worked for me. I just had to add - (IBAction)onButtonTouchDragOutside:(UIButton *)sender { to make sure the colour doesn't remain on when the user accidentally drags his finger off the button.

@Stunner 2015-05-27 00:27:27

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

@ma11hew28 2014-04-11 16:44:19

UPDATE:

Use the UIButtonBackgroundColor Swift library.

OLD:

Use the helpers below to create a 1 px x 1 px image with a grayscale fill color:

UIImage *image = ACUTilingImageGray(248/255.0, 1);

or an RGB fill color:

UIImage *image = ACUTilingImageRGB(253/255.0, 123/255.0, 43/255.0, 1);

Then, use that image to set the button's background image:

[button setBackgroundImage:image forState:UIControlStateNormal];

Helpers

#pragma mark - Helpers

UIImage *ACUTilingImageGray(CGFloat gray, CGFloat alpha)
{
    return ACUTilingImage(alpha, ^(CGContextRef context) {
        CGContextSetGrayFillColor(context, gray, alpha);
    });
}

UIImage *ACUTilingImageRGB(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)
{
    return ACUTilingImage(alpha, ^(CGContextRef context) {
        CGContextSetRGBFillColor(context, red, green, blue, alpha);
    });
}

UIImage *ACUTilingImage(CGFloat alpha, void (^setFillColor)(CGContextRef context))
{
    CGRect rect = CGRectMake(0, 0, 0.5, 0.5);
    UIGraphicsBeginImageContextWithOptions(rect.size, alpha == 1, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    setFillColor(context);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

Note: ACU is the class prefix of my Cocoa Touch Static Library called Acani Utilities, where AC is for Acani, and U is for Utilities.

@Fostah 2015-04-06 19:17:19

Here's an approach in Swift, using a UIButton extension to add an IBInspectable, called highlightedBackgroundColor. Similar to subclassing, without requiring a subclass.

private var HighlightedBackgroundColorKey = 0
private var NormalBackgroundColorKey = 0

extension UIButton {

    @IBInspectable var highlightedBackgroundColor: UIColor? {
        get {
            return objc_getAssociatedObject(self, &HighlightedBackgroundColorKey) as? UIColor
        }

        set(newValue) {
            objc_setAssociatedObject(self,
                &HighlightedBackgroundColorKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN))
        }
    }

    private var normalBackgroundColor: UIColor? {
        get {
            return objc_getAssociatedObject(self, &NormalBackgroundColorKey) as? UIColor
        }

        set(newValue) {
            objc_setAssociatedObject(self,
                &NormalBackgroundColorKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN))
        }
    }

    override public var backgroundColor: UIColor? {
        didSet {
            if !highlighted {
                normalBackgroundColor = backgroundColor
            }
        }
    }

    override public var highlighted: Bool {
        didSet {
            if let highlightedBackgroundColor = self.highlightedBackgroundColor {
                if highlighted {
                    backgroundColor = highlightedBackgroundColor
                } else {
                    backgroundColor = normalBackgroundColor
                }
            }
        }
    }
}

I hope this helps.

@Eli Burke 2016-01-05 16:03:49

For swift 2.0, you will need to update the call to objc_setAssociatedObject to use an enum: objc_setAssociatedObject(self, &NormalBackgroundColorKey, newValue, .OBJC_ASSOCIATION_RETAIN)

@davidethell 2016-01-22 11:28:45

Definitely best way in Swift if you want to keep it all in Storyboard.

@Hossam Ghareeb 2016-04-27 12:25:40

I prefer using subclass not extension as this will affect the whole app

@mash 2015-02-03 07:51:26

Override highlighted variable. Adding @IBInspectable makes you edit the highlighted backgroundColor in storyboard, which is nifty too.

class BackgroundHighlightedButton: UIButton {
    @IBInspectable var highlightedBackgroundColor :UIColor?
    @IBInspectable var nonHighlightedBackgroundColor :UIColor?
    override var highlighted :Bool {
        get {
            return super.highlighted
        }
        set {
            if newValue {
                self.backgroundColor = highlightedBackgroundColor
            }
            else {
                self.backgroundColor = nonHighlightedBackgroundColor
            }
            super.highlighted = newValue
        }
    }
}

@Damien Romito 2015-01-22 16:00:25

You can use this category which add the method setBackgroundColor:forState:

https://github.com/damienromito/UIButton-setBackgroundColor-forState-

@gnasher729 2015-10-26 13:30:37

Huge advantage that it doesn't require subclassing.

@Recycled Steel 2014-12-18 14:07:08

You can subclass the UIButton and make a nice forState.

colourButton.h

#import <UIKit/UIKit.h>

@interface colourButton : UIButton

-(void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state;

@end

colourButton.m

#import "colourButton.h"

@implementation colourButton
{
    NSMutableDictionary *colours;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    // If colours does not exist
    if(!colours)
    {
        colours = [NSMutableDictionary new];  // The dictionary is used to store the colour, the key is a text version of the ENUM
        colours[[NSString stringWithFormat:@"%lu", UIControlStateNormal]] = (UIColor*)self.backgroundColor;  // Store the original background colour
    }

    return self;
}

-(void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state
{
    // If it is normal then set the standard background here
    if(state & UIControlStateNormal)
    {
        [super setBackgroundColor:backgroundColor];
    }

    // Store the background colour for that state
    colours[[NSString stringWithFormat:@"%lu", state]]= backgroundColor;
}

-(void)setHighlighted:(BOOL)highlighted
{
    // Do original Highlight
    [super setHighlighted:highlighted];

    // Highlight with new colour OR replace with orignial
    if (highlighted && colours[[NSString stringWithFormat:@"%lu", UIControlStateHighlighted]])
    {
        self.backgroundColor = colours[[NSString stringWithFormat:@"%lu", UIControlStateHighlighted]];
    }
    else
    {
        self.backgroundColor = colours[[NSString stringWithFormat:@"%lu", UIControlStateNormal]];
    }
}

-(void)setSelected:(BOOL)selected
{
    // Do original Selected
    [super setSelected:selected];

    // Select with new colour OR replace with orignial
    if (selected && colours[[NSString stringWithFormat:@"%lu", UIControlStateSelected]])
    {
        self.backgroundColor = colours[[NSString stringWithFormat:@"%lu", UIControlStateSelected]];
    }
    else
    {
        self.backgroundColor = colours[[NSString stringWithFormat:@"%lu", UIControlStateNormal]];
    }
}

@end

Notes (This is an example, I know there are problems and here are some)

I have used an NSMutableDictionay to store the UIColor for each State, I have to do a nasty text conversion for the Key as the UIControlState is not a nice straight Int. If it where you could init an Array with that many objects and use the State as an index.

Because of this you many have difficulties with e.g. a selected & disabled button, some more logic is needed.

Another problem is if you try and set multiple colours at the same time, I have not tried with a button but if you can do this it may not work

 [btn setBackgroundColor:colour forState:UIControlStateSelected & UIControlStateHighlighted];

I have assumed this is StoryBoard, there is no init, initWithFrame so add them if you need them.

@Siby 2013-01-25 14:26:55

Try this if you have an image:

-(void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

or see if showsTouchWhenHighlighted is enough for you.

@MartinMoizard 2013-01-25 14:31:25

I tried playing around with showsTouchWhenHighlighted but it didn't help. I don't want to use setBackgroundImage:forState:. I was in fact trying to use the backgroundColor to not use any image.

@jjv360 2013-01-25 14:06:28

Try tintColor:

_button.tintColor = [UIColor redColor];

@MartinMoizard 2013-01-25 14:26:15

I just tried, it's not working

@jjv360 2013-01-25 15:05:26

Are you sure it's linked in IB? What do you get if you do NSLog(@"%@", _button);?

@JaredH 2014-06-25 11:40:05

This won't work if you're using a UIButtonTypeCustom.

Related Questions

Sponsored Content

34 Answered Questions

[SOLVED] How do I create a basic UIButton programmatically?

51 Answered Questions

[SOLVED] How to change Status Bar text color in iOS

6 Answered Questions

[SOLVED] Change UIButton border color on highlight

7 Answered Questions

0 Answered Questions

6 Answered Questions

[SOLVED] Change background color of UIButton when Highlighted

  • 2010-10-26 10:26:33
  • iPhoneDev
  • 45868 View
  • 26 Score
  • 6 Answer
  • Tags:   iphone uibutton

2 Answered Questions

[SOLVED] UIButton – what is the default image highlight color

1 Answered Questions

UIButton adjustsImageWhenHighlighted Property With Background Color

  • 2013-06-16 11:44:14
  • motionpotion
  • 2942 View
  • 2 Score
  • 1 Answer
  • Tags:   ios xcode uibutton

1 Answered Questions

[SOLVED] change button background color when clicked

Sponsored Content