我创建了一个UIButton:
testNavigationButton.setTitle("Test", forState: .Normal)
testNavigationButton.layer.borderWidth = 1
我尝试将颜色更改为:
testNavigationButton.layer.borderColor = UIColor.blueColor().CGColor
但是边框的颜色与用于设置按钮标题的默认阴影的颜色不同。什么是阴影,如何将边框的颜色更改为此颜色?
答案 0 :(得分:2)
您可以使用
获取文字的颜色let textColor = button.titleLabel?.textColor
并将它分配给你的borderColor:
button.layer.borderColor = textColor!.CGColor
顺便说一下,在我的测试中,蓝色是#007aff
你可以使用以下方式初始化它:
let color = UIColor(red: 0, green: 0.478, blue: 1, alpha: 1)
<强>更新强>
class MyButton: UIButton {
private let normalColor = UIColor(red: 0, green: 0.478, blue: 1, alpha: 1)
private let highlightedColor = UIColor(red: 0, green: 0.478, blue: 1, alpha: 0.3)
override var highlighted: Bool {
willSet {
if newValue != highlighted {
let anim = CABasicAnimation(keyPath: "borderColor")
anim.fromValue = newValue ? normalColor.CGColor : highlightedColor.CGColor
anim.toValue = newValue ? highlightedColor.CGColor : normalColor.CGColor
layer.borderColor = newValue ? highlightedColor.CGColor : normalColor.CGColor
anim.duration = CATransaction.animationDuration()
anim.timingFunction = CATransaction.animationTimingFunction()
layer.addAnimation(anim, forKey: "MyButton.BorderColor")
}
}
}
}