How to set the custom border color of UIView programmatically?

8 Solutions Collect From Internet About “How to set the custom border color of UIView programmatically?”

If you want to customize the color, use the code below…

If you Use Swift 2.0-

self.yourView.layer.borderWidth = 1
self.yourView.layer.borderColor = UIColor(red:222/255.0, green:225/255.0, blue:227/255.0, alpha: 1.0).cgColor

If you Use Swift 3.1 or latter –

 self.yourView.layer.borderWidth = 1
 self.yourView.layer.borderColor = UIColor.init(red:222/255.0, green:225/255.0, blue:227/255.0, alpha: 1.0).cgColor

You can set UIView’s border width and border color using below code.

yourView.layer.borderWidth = 1

yourView.layer.borderColor =

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 _गोल_करा(radius:CGFloat, color:UIColor = UIColor.clearColor()) -> UIView{
        var rounfView:UIView = self
        rounfView.layer.cornerRadius = CGFloat(radius)
        rounfView.layer.borderWidth = 1
        rounfView.layer.borderColor = color.CGColor
        rounfView.clipsToBounds = true
        return rounfView


btnLogin._गोल_करा(7, color: UIColor.lightGrayColor())

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:

class CustomView: UIView{

@IBInspectable var borderWidth: CGFloat = 0.0{


        self.layer.borderWidth = borderWidth

@IBInspectable var borderColor: UIColor = UIColor.clear {

    didSet {

        self.layer.borderColor = borderColor.cgColor

override func prepareForInterfaceBuilder() {



Allows easy modification from Interface Builder:

Interface Builder

Write the code in your viewDidLoad()

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

And you can set Color with 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 in UIKit

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

Swift 3.0

       groundTrump.layer.borderColor =

swift 3

func borderColor(){

    self.viewMenuItems.layer.cornerRadius = 13
    self.viewMenuItems.layer.borderWidth = 1
    self.viewMenuItems.layer.borderColor = UIColor.white.cgColor