我在用户定义的运行时属性中为 UIbutton 设置了 CornerRadius 和 BorderWidth。在不添加 layer.borderColor 的情况下,它可以正常工作并以黑色显示边框。但是当添加 layer.borderColor 时不起作用(不显示边框)。
最佳答案
对于 Swift:
swift 3:
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}
swift 2.2:
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(CGColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.CGColor
}
}
}
关于ios - 更改 Storyboard 中的 UIButton BorderColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28854469/