我想知道如何在我的 UIButton
周围设置边框,以便在单击或突出显示时更改不透明度及其中的文本。
我的逻辑告诉我,它应该是这样的......但它似乎不起作用:
//BTN STYLING
btnstd.layer.cornerRadius = 5
btnstd.layer.borderWidth = 1.5
btnstd.layer.borderColor = UIColor.white.cgColor
//Change bordercolor when highlighted
if(btnstd.isHighlighted) {
btnstd.layer.borderColor = UIColor(white:1,alpha:0.3).cgColor
}
顺便说一句,这是放在我的 ViewDidLoad()
函数中的
最佳答案
您正在寻找的操作是 .touchDown
和任何 .touchUp
:
override func viewDidLoad() {
super.viewDidLoad()
theButton.setTitle("Normal", for: .normal)
theButton.setTitle("Highlighted", for: .highlighted)
theButton.layer.borderColor = UIColor.red.cgColor
theButton.layer.borderWidth = 1
theButton.addTarget(self, action: #selector(startHighlight), for: .touchDown)
theButton.addTarget(self, action: #selector(stopHighlight), for: .touchUpInside)
theButton.addTarget(self, action: #selector(stopHighlight), for: .touchUpOutside)
}
func startHighlight(sender: UIButton) {
theButton.layer.borderColor = UIColor.green.cgColor
theButton.layer.borderWidth = 1
}
func stopHighlight(sender: UIButton) {
theButton.layer.borderColor = UIColor.red.cgColor
theButton.layer.borderWidth = 1
}
关于swift - Button.isHighlighted 时如何更改边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40272274/