How to change the background color of a UIButton w

2019-01-01 09:53发布

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条回答
琉璃瓶的回忆
2楼-- · 2019-01-01 10:19

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

查看更多
临风纵饮
3楼-- · 2019-01-01 10:21

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];    
}
查看更多
看风景的人
4楼-- · 2019-01-01 10:22

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

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

查看更多
情到深处是孤独
5楼-- · 2019-01-01 10:23

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)
查看更多
初与友歌
6楼-- · 2019-01-01 10:23

Try tintColor:

_button.tintColor = [UIColor redColor];
查看更多
栀子花@的思念
7楼-- · 2019-01-01 10:24

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
        }
    }
}
查看更多
登录 后发表回答