简体   繁体   English

更改UIControlState.Highlighted上的UIButton backgroundColor吗?

[英]Change UIButton backgroundColor on UIControlState.Highlighted?

I have the following UIButton: 我有以下UIButton:

// create footerView
let footerView: UIView = UIView()
footerView.backgroundColor = UIColor.yellowColor()
self.view.addSubview(footerView)

footerView.snp_makeConstraints { (make) -> Void in
  make.left.equalTo(self.view.snp_left)
  make.right.equalTo(self.view.snp_right)
  make.height.equalTo(self.footerHeight)
  make.bottom.equalTo(self.view.snp_bottom)
}

let footerButton: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
footerButton.setTitle("OK", forState: UIControlState.Normal)
footerButton.backgroundColor = UIColor.redColor()
footerButton.tintColor = UIColor.whiteColor()
footerButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
footerView.addSubview(footerButton)

footerButton.snp_makeConstraints { (make) -> Void in
  make.center.equalTo(footerView)
  make.left.equalTo(footerView.snp_left).offset(45)
  make.right.equalTo(footerView.snp_right).offset(-45)
}

Edit: I could use 编辑:我可以使用

footerButton.setBackgroundImage(redImage, forState: UIControlState.Highlighted)

but this would destroy the auto layout and border radius if I tapp the button. 但这会破坏自动布局和边框半径,如果我点击该按钮。

How can I change the backgroundColor of the button on UIControlState.Highlighted ? 如何更改UIControlState.Highlighted按钮的backgroundColor?

There is no -setBackgroundColor:forState method. 没有-setBackgroundColor:forState方法。 You can create a solid color background image (programatically or included in your resources) though and use 不过,您可以创建纯色背景图像(以编程方式或包含在资源中)并使用

- setBackgroundImage:forState:

@stephan1001 I have tried this code and it works @ stephan1001我已经尝试过此代码,并且可以正常工作

class ViewController: UIViewController {

var footerHeight = 70.0

override func viewDidLoad() {
    super.viewDidLoad()
    configUI()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func configUI(){
    // create footerView
    let footerView: UIView = UIView()
    footerView.backgroundColor = UIColor.yellowColor()
    self.view.addSubview(footerView)

    footerView.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(self.view.snp_left)
        make.right.equalTo(self.view.snp_right)
        make.height.equalTo(self.footerHeight)
        make.bottom.equalTo(self.view.snp_bottom)
    }

    let footerButton: MyButton = MyButton.buttonWithType(UIButtonType.Custom) as! MyButton
    footerButton.setTitle("OK", forState: UIControlState.Normal)
    footerButton.backgroundColor = UIColor.redColor()
    footerButton.tintColor = UIColor.whiteColor()
    footerButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
    footerView.addSubview(footerButton)

    footerButton.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(footerView)
        make.left.equalTo(footerView.snp_left).offset(45)
        make.right.equalTo(footerView.snp_right).offset(-45)
    }
}


}

class MyButton: UIButton{
    override var highlighted: Bool {
        willSet(newValue) {
            if ( newValue == true ){
                self.backgroundColor = UIColor.greenColor()
            } else{
                self.backgroundColor = UIColor.redColor()
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM