ios - 无法在 UIImageView 上设置边框

标签 ios swift uiimageview

<分区>

我已经尝试了很多次,只是为了在 ImageView 上获得一个简单的边框,但似乎没有任何效果。这些是我现在的事件设置:

Settings

但是结果还是没有任何边框。

No border

我的设置中缺少什么?

最佳答案

layer.borderColor 是一个 CGColorRef。但是界面生成器传递了一个 UIColor。您有两个选择:

1 - 在代码中设置边框颜色:

self.imageView.layer.borderColor = UIColor.blue.cgColor

2 - 使用 borderColor 属性扩展 UIImageViewUIView 并使用 @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
        }
    }
}

相关文章:

https://stackoverflow.com/a/17993890/3814799

https://stackoverflow.com/a/35372610/3814799

关于ios - 无法在 UIImageView 上设置边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48715294/

相关文章:

ios - 缩略图在 iPhone 或 iPad 上不显示,但在 Android 上显示

ios - UIImageView 在边框上拉伸(stretch)/重复图像并保持其质量

iphone - 为 iOS 设备构建时为 "CoreFoundation.h: Not found",但对于模拟器来说没问题

ios - 从 CloudKit 获取不在设备上的记录的任何(或最佳)方法?

ios - encodeWithCoder 错误

arrays - swift 2.2 : Non +1 increasing iterator loops

快速序列变形

ios - 选择单元格时 ImageView 消失但 UILabel 不消失

ios - 在 iOS 中为序列设置动画的最佳方式

ios - 如何更改 UITableView 背景图片的大小?