swift - Button.isHighlighted 时如何更改边框颜色

标签 swift

我想知道如何在我的 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/

相关文章:

ios - dateFromString 给出 nil,swift

ios - 找不到接受提供的参数的 "init"的重载

ios - 成功后切换回启动窗口

ios - 为什么分步应用的 CGAffineTransform 与一次应用的行为不同?

ios - 恢复应用程序状态时显示不确定的事件指示器?

swift - 试图了解 URLSession 身份验证挑战

swift - 点击 Today Widget 启动 App

swift - 原生swift zip解压

c++ - Xcode 7.1 和 C++

不同屏幕尺寸的 iOS 缩放约束