How to set the custom border color of UIView programmatically?

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

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

10条回答
  •  温柔的废话
    2020-12-23 03:32

    You can write an extension to use it with all the UIViews eg. UIButton, UILabel, UIImageView etc. You can customise my following method as per your requirement, but I think it will work well for you.

    extension UIView{
    
        func setBorder(radius:CGFloat, color:UIColor = UIColor.clearColor()) -> UIView{
            var roundView:UIView = self
            roundView.layer.cornerRadius = CGFloat(radius)
            roundView.layer.borderWidth = 1
            roundView.layer.borderColor = color.CGColor
            roundView.clipsToBounds = true
            return roundView
        }
    }
    

    Usage:

    btnLogin.setBorder(7, color: UIColor.lightGrayColor())
    imgViewUserPick.setBorder(10)
    

提交回复
热议问题