无法在 UIImageView 上设置边框
Unable to set border on UIImageView
我已经尝试了很多次,只是为了在 ImageView 上获得一个简单的边框,但似乎没有任何效果。这些是我现在的活动设置:
但是结果还是没有边框
我的设置中缺少什么?
layer.borderColor
是一个 CGColorRef
。但是界面生成器传递了 UIColor
。您有两个选择:
1 - 在代码中设置边框颜色:
self.imageView.layer.borderColor = UIColor.blue.cgColor
2 - 使用 borderColor
属性 扩展 UIImageView
或 UIView
并使用 @IBDesignable
和 @IBInspectable
以便 属性 在界面生成器中可见。使用这个新的 属性 作为 layer.borderColor
.
的代理
@IBDesignable
extension UIView {
@IBInspectable
var borderColor: UIColor? {
get {
if let cgColor = self.layer.borderColor {
return UIColor(cgColor: cgColor)
}
return nil
} set {
self.layer.borderColor = newValue?.cgColor
}
}
}
相关帖子:
我已经尝试了很多次,只是为了在 ImageView 上获得一个简单的边框,但似乎没有任何效果。这些是我现在的活动设置:
但是结果还是没有边框
我的设置中缺少什么?
layer.borderColor
是一个 CGColorRef
。但是界面生成器传递了 UIColor
。您有两个选择:
1 - 在代码中设置边框颜色:
self.imageView.layer.borderColor = UIColor.blue.cgColor
2 - 使用 borderColor
属性 扩展 UIImageView
或 UIView
并使用 @IBDesignable
和 @IBInspectable
以便 属性 在界面生成器中可见。使用这个新的 属性 作为 layer.borderColor
.
@IBDesignable
extension UIView {
@IBInspectable
var borderColor: UIColor? {
get {
if let cgColor = self.layer.borderColor {
return UIColor(cgColor: cgColor)
}
return nil
} set {
self.layer.borderColor = newValue?.cgColor
}
}
}
相关帖子: