【发布时间】:2017-03-14 10:17:52
【问题描述】:
【问题讨论】:
标签: ios objective-c swift xcode uiview
【问题讨论】:
标签: ios objective-c swift xcode uiview
您需要使用 Button 的图层来设置边框属性。例如:
button.layer.cornerRadius = button.frame.height / 2.0
button.layer.masksToBounds = true
button.layer.borderColor = UIColor.lightGray.cgColor
button.layer.borderWidth = 1.0
或
添加此扩展以在情节提要中获取这些选项。
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 {
let color = UIColor.init(cgColor: layer.borderColor!)
return color
}
set {
layer.borderColor = newValue?.cgColor
}
}
@IBInspectable var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0, height: 2)
layer.shadowOpacity = 0.4
layer.shadowRadius = shadowRadius
}
}
}
【讨论】:
UIView extension 以在情节提要中获取这些选项。检查更新的答案。
您不能将 CALayer 属性直接设置为 UIElement。相反,需要使用 .layer 属性。
例子:
button.layer.cornerRadius = button.frame.height/ 2.0
button.layer.maskToBounds = true
【讨论】:
应该是这样的
#import <QuartzCore/QuartzCore.h>
[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor greenColor].CGColor];
myButton.layer.cornerRadius = myButton.frame.height / 2.0;
供参考:
【讨论】: