How to set the custom border color of UIView programmatically?

后端 未结 10 503
情书的邮戳
情书的邮戳 2020-12-23 02:29

I am trying to set the custom border color of UIView programmatically in Swift.

10条回答
  •  星月不相逢
    2020-12-23 03:23

    Use @IBDesignable and @IBInspectable to do the same.

    They are re-useable, easily modifiable from the Interface Builder and the changes are reflected immediately in the Storyboard

    Conform the objects in the storyboard to the particular class

    Code Snippet:

    @IBDesignable
    class CustomView: UIView{
    
    @IBInspectable var borderWidth: CGFloat = 0.0{
    
        didSet{
    
            self.layer.borderWidth = borderWidth
        }
    }
    
    
    @IBInspectable var borderColor: UIColor = UIColor.clear {
    
        didSet {
    
            self.layer.borderColor = borderColor.cgColor
        }
    }
    
    override func prepareForInterfaceBuilder() {
    
        super.prepareForInterfaceBuilder()
    }
    
    }
    

    Allows easy modification from Interface Builder:

提交回复
热议问题