How to change the highlighted color of a UIButton?

87,976

Solution 1

Try to Override the UIButton with the following Method.. and just change the backgroud color of button when its in highlighted state.

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];

    if (highlighted) {
        self.backgroundColor = [UIColor Your Customcolor];
    }
    else{
        self.backgroundColor = [UIColor Your DefaultColor];
    }   

}

Try it..hope it helps

Solution 2

You can use setBackgroundImage:forState: to set the background image for the button when highlighted.

ex:

As suggested by Tim in their answer here, you can create aUIImage from UIColor:

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

Then set image as button's background image when highlighted

[button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateHighlighted];

Solution 3

In Swift:

import UIKit

class CustomUIButtonForUIToolbar: UIButton {

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        super.drawRect(rect)

        self.layer.borderColor = UIColor.blueColor().CGColor
        self.layer.borderWidth = 1.0
        self.layer.cornerRadius = 5.0
        self.clipsToBounds = true
        self.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)

        self.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
    }

    override var highlighted: Bool {
        didSet {

            if (highlighted) {
                self.backgroundColor = UIColor.blueColor()
            }
            else {
                self.backgroundColor = UIColor.clearColor()
            }

        }
    }

}

Solution 4

I'll provide a Swift answer, which can be used without any customization, if you want the highlighted color to be a darkened version of the original background color. If you, on the other hand, want a completely different highlighted background color, you can supply that to the highlightedBackgroundColor property as a UIColor.

The following is the most efficient and straightforward way to implement such a functionality in Swift. It is also generic, meaning it can be used for lots of different buttons with different colors.

Here's the code:

import UIKit

class HighlightedColorButton: UIButton {

    // A new highlightedBackgroundColor, which shows on tap
    var highlightedBackgroundColor: UIColor?
    // A temporary background color property, which stores the original color while the button is highlighted
    var temporaryBackgroundColor: UIColor?


    // Darken a color
    func darkenColor(color: UIColor) -> UIColor {

        var red = CGFloat(), green = CGFloat(), blue = CGFloat(), alpha = CGFloat()

        color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

        red = max(red - 0.5, 0.0)
        green = max(green - 0.5, 0.0)
        blue = max(blue - 0.5, 0.0)

        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }


    // Set up a property observer for the highlighted property, so the color can be changed
    @objc override var highlighted: Bool {
        didSet {
            if highlighted {
                if temporaryBackgroundColor == nil {
                    if backgroundColor != nil {
                        if let highlightedColor = highlightedBackgroundColor {
                            temporaryBackgroundColor = backgroundColor
                            backgroundColor = highlightedColor
                        } else {
                            temporaryBackgroundColor = backgroundColor
                            backgroundColor = darkenColor(temporaryBackgroundColor!)
                        }
                    }
                }
            } else {
                if let temporaryColor = temporaryBackgroundColor {
                    backgroundColor = temporaryColor
                    temporaryBackgroundColor = nil
                }
            }
        }
    }
}

Treat the button as a normal UIButton, with the addition of the optional property highlightedBackgroundColor:

let highlightedColorButton = HighlightedColorButton.buttonWithType(.Custom) as HighlightedColorButton
highlightedColorButton.backgroundColor = UIColor.redColor()
highlightedColorButton.highlightedBackgroundColor = UIColor.blueColor()

Solution 5

Use this statement to set the highlighted color of the UIButton:

[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
Share:
87,976
Code Monkey
Author by

Code Monkey

Updated on September 19, 2020

Comments

  • Code Monkey
    Code Monkey over 3 years

    I created a navigation button in my UINavigationController. I set it to be highlighted when touched:

    [someButton setShowsTouchWhenHighlighted:YES];
    

    Is there a way to change the highlighted color to something other than the default white?

  • Leslie Godwin
    Leslie Godwin about 10 years
    Ya, this is really the only way to change the highlighted background colour without overriding the control.
  • Clement Prem
    Clement Prem over 9 years
  • Brian Sachetta
    Brian Sachetta about 9 years
    This is good but I'd recommend also resetting the background color when the button is not highlighted.
  • Brian Sachetta
    Brian Sachetta about 9 years
    Example: self.backgroundColor = highlighted ? [UIColor darkGrayColor] : [UIColor blueColor];
  • Christopher Swasey
    Christopher Swasey over 7 years
    FYI none of that code should be in drawRect. drawRect is for drawing to a graphics context. I'd personally put those into awakeFromNib but init?(coder:) also works.
  • Claudio Redi
    Claudio Redi almost 7 years
    It seems you copied it from @ClementPrem link... this question should be marked as duplicated but in worst case it wouldn't hurt to give credit to owner...