Cocoa Touch: How To Change UIView's Border Color And Thickness?

后端 未结 14 1091
心在旅途
心在旅途 2020-11-30 16:56

I saw in the inspector that I can change the background color, but I\'d like to also change the border color and thickness, is this possible?

14条回答
  •  难免孤独
    2020-11-30 17:30

    @IBInspectable is working for me on iOS 9 , Swift 2.0

    extension UIView {
    
    @IBInspectable var borderWidth: CGFloat {
    get {
            return layer.borderWidth
        }
        set(newValue) {
            layer.borderWidth = newValue
        }
    }
    
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set(newValue) {
            layer.cornerRadius = newValue
        }
    }
    
    @IBInspectable var borderColor: UIColor? {
        get {
            if let color = layer.borderColor {
                return UIColor(CGColor: color)
            }
            return nil
        }
        set(newValue) {
            layer.borderColor = newValue?.CGColor
        }
    }
    

提交回复
热议问题