How to set custom border color of UIView programmatically?

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

+84
ios swift
Apr 17 '15 at 13:41
source share
10 answers

If you are using Swift 2.0.0+

self.yourView.layer.borderWidth = 1 self.yourView.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor 
+179
Apr 17 '15 at 13:55
source share

In Swift 4, you can set the border color and width for UIControls using the code below.

 let yourColor : UIColor = UIColor( red: 0.7, green: 0.3, blue:0.1, alpha: 1.0 ) yourControl.layer.masksToBounds = true yourControl.layer.borderColor = yourColor.CGColor yourControl.layer.borderWidth = 1.0 

<Swift 4 , you can set the border width of the UIView and the border color using the code below.

 yourView.layer.borderWidth = 1 yourView.layer.borderColor = UIColor.red.cgColor 
+76
Apr 17 '15 at 13:46 on
source share

Use @IBDesignable and @IBInspectable to do the same.

They can be reused, easily modified from Interface Builder, and the changes are immediately reflected in the storyboard

Match the objects in the storyboard to a specific 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 you to easily modify the Interface Builder interface:

Interface builder

+15
Nov 15 '16 at 4:57
source share

You can write an extension to use it with all UIViews, for example. UIButton, UILabel, UIImageView, etc. You can customize my next method to suit your requirements, 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 } } 

Using:

 btnLogin.setBorder(7, color: UIColor.lightGrayColor()) imgViewUserPick.setBorder(10) 
+12
Apr 17 '15 at 14:16
source share

swift 3

 func borderColor(){ self.viewMenuItems.layer.cornerRadius = 13 self.viewMenuItems.layer.borderWidth = 1 self.viewMenuItems.layer.borderColor = UIColor.white.cgColor } 
+8
Sep 21 '17 at 11:24 on
source share

Enter the code in viewDidLoad()

 self.view.layer.borderColor = anyColor().CGColor 

And you can set Color using RGB

 func anyColor() -> UIColor { return UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0) } 

Learn something about CALayer at UIKit

+3
Apr 17 '15 at 13:53 on
source share

swift 3.0

 self.uiTextView.layer.borderWidth = 0.5 self.txtItemShortDes.layer.borderColor = UIColor(red:205.0/255.0, green:205.0/255.0, blue:205.0/255.0, alpha: 1.0).cgColor 
+3
Jan 27 '17 at 12:20
source share

We can create a method for this. Just use it.

 public func createBorderForView(color: UIColor, radius: CGFloat, width: CGFloat = 0.7) { self.layer.borderWidth = width self.layer.cornerRadius = radius self.layer.shouldRasterize = false self.layer.rasterizationScale = 2 self.clipsToBounds = true self.layer.masksToBounds = true let cgColor: CGColor = color.cgColor self.layer.borderColor = cgColor } 
+3
Mar 08 '18 at 18:20
source share

Swift 3.0

  groundTrump.layer.borderColor = UIColor.red.cgColor 
+2
October 13 '16 at 20:23
source share

Swift 5.2 , UIView + extension

 extension UIView { public func addViewBorder(borderColor:CGColor,borderWith:CGFloat,borderCornerRadius:CGFloat){ self.layer.borderWidth = borderWith self.layer.borderColor = borderColor self.layer.cornerRadius = borderCornerRadius } } 

You used this extension;

 yourView.addViewBorder(borderColor: #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1), borderWith: 1.0, borderCornerRadius: 20) 
0
Jun 14 '19 at 22:12
source share



All Articles