我有以下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)
}

编辑:我可以使用
footerButton.setBackgroundImage(redImage, forState: UIControlState.Highlighted)

但这会破坏自动布局和边框半径,如果我点击该按钮。

如何更改UIControlState.Highlighted上按钮的backgroundColor?

最佳答案

没有-setBackgroundColor:forState方法。不过,您可以创建纯色背景图像(以编程方式或包含在资源中)并使用

- setBackgroundImage:forState:

关于ios - 更改UIControlState.Highlighted上的UIButton backgroundColor吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30897561/

10-10 23:14