Skip to content

Instantly share code, notes, and snippets.

@MrJackdaw
Last active May 12, 2023 19:02
Show Gist options
  • Save MrJackdaw/6ffbc33fc274838412bfe3ad48592b9b to your computer and use it in GitHub Desktop.
Save MrJackdaw/6ffbc33fc274838412bfe3ad48592b9b to your computer and use it in GitHub Desktop.
Swift 3 Extension for adding border to one side of UIView
// This syntax reflects changes made to the Swift language as of Aug. '16
extension UIView {
// Example use: myView.addBorder(toSide: .Left, withColor: UIColor.redColor().CGColor, andThickness: 1.0)
enum ViewSide {
case Left, Right, Top, Bottom
}
func addBorder(toSide side: ViewSide, withColor color: CGColor, andThickness thickness: CGFloat) {
let border = CALayer()
border.backgroundColor = color
switch side {
case .Left: border.frame = CGRect(x: frame.minX, y: frame.minY, width: thickness, height: frame.height); break
case .Right: border.frame = CGRect(x: frame.maxX, y: frame.minY, width: thickness, height: frame.height); break
case .Top: border.frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: thickness); break
case .Bottom: border.frame = CGRect(x: frame.minX, y: frame.maxY, width: frame.width, height: thickness); break
}
layer.addSublayer(border)
}
}
@cmoyc
Copy link

cmoyc commented Dec 27, 2017

Really good, men it's a easy implementation, and I love it, it's pretty useful thanks again

@stuyam
Copy link

stuyam commented Jan 2, 2018

This is great thanks! One note, I find it easier to pass everything in as a UIColor and convert to cgColor internally in the method.

@mvnrc
Copy link

mvnrc commented Feb 5, 2018

switch side {
case .Left: border.frame = CGRect(x: frame.minX, y: frame.minY, width: thickness, height: frame.height); break
case .Right: border.frame = CGRect(x: frame.maxX - thickness, y: frame.minY, width: thickness, height: frame.height); break
case .Top: border.frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: thickness); break
case .Bottom: border.frame = CGRect(x: frame.minX, y: frame.maxY - thickness, width: frame.width, height: thickness); break
}

@idrougge
Copy link

break serves no purpose here.

@jbeenie
Copy link

jbeenie commented Feb 13, 2018

This should be in UIKit!

@hemangshah
Copy link

hemangshah commented Feb 14, 2018

Another version:

extension UIView {
    
    // Example use: myView.addBorder(toSide: .Left, withColor: UIColor.redColor().CGColor, andThickness: 1.0)

    enum ViewSide {
        case left, right, top, bottom
    }
    
    func addBorder(toSide side: ViewSide, withColor color: CGColor, andThickness thickness: CGFloat) {
        
        let border = CALayer()
        border.backgroundColor = color
        
        switch side {
        case .left: border.frame = CGRect(x: frame.minX, y: frame.minY, width: thickness, height: frame.height)
        case .right: border.frame = CGRect(x: frame.maxX, y: frame.minY, width: thickness, height: frame.height)
        case .top: border.frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: thickness)
        case .bottom: border.frame = CGRect(x: frame.minX, y: frame.maxY, width: frame.width, height: thickness)
        }
        
        layer.addSublayer(border)
    }
}

@tmikevshoprunner
Copy link

This needs to be updated to be using border instead of frame. It currently only works properly in the special case where border == frame.

@naveedahmad99
Copy link

@hemangshah whats the difference in the updated version?

@mdstrapa
Copy link

it works perfectly! thanks e congrats

@salvationarinze
Copy link

This doesn't work on all screen sizes. On bigger screens, it doesn't extend to the end of the border

@Kyle0021
Copy link

Kyle0021 commented Feb 1, 2019

Works lovely. Used it on a UICollectionViewCell. Many thanks.

@iOSVinod
Copy link

Working lovely in UiTextField with some changes in switch block code:

switch side {
case .left: border.frame = CGRect(x:bounds.minX, y: bounds.minY, width: thickness, height: bounds.height)
case .right: border.frame = CGRect(x: bounds.maxX, y: bounds.minY, width: thickness, height: bounds.height)
case .top: border.frame = CGRect(x: bounds.minX, y: bounds.minY, width: bounds.width, height: thickness)
case .bottom: border.frame = CGRect(x: bounds.minX, y: bounds.maxY, width: bounds.width, height: thickness)
}

@bcapetillo
Copy link

this code worked for my..... good luck
thanks
`import Foundation
import UIKit

// This syntax reflects changes made to the Swift language as of Aug. '16
extension UIView {

// Example use: myView.addBorder(toSide: .Left, withColor: UIColor.redColor().CGColor, andThickness: 1.0)

enum ViewSide {
    case Left, Right, Top, Bottom
}

func addBorder(toSide side: ViewSide, withColor color: CGColor, andThickness thickness: CGFloat) {
    
    let border = CALayer()
    border.backgroundColor = color
    switch side {
    case .Left: border.frame = CGRect(x: 0.0, y: 0.0, width: thickness, height: frame.height); break
    case .Right: border.frame = CGRect(x: frame.width-thickness, y: 0.0, width: thickness, height: frame.height); break
    case .Top: border.frame = CGRect(x: 0.0, y: 0.0, width: frame.width, height: thickness); break
    case .Bottom: border.frame = CGRect(x: 0.0, y: frame.height-thickness, width: frame.width, height: thickness); break
    }
    
    layer.addSublayer(border)
}

}
`

@koenvanderdrift
Copy link

koenvanderdrift commented Apr 6, 2020

Also works for NSView. Only need to swap top and bottom, since the coordinate system origin on macOS is in the left bottom of a view.

And per Swift naming guidelines, case names should be lowerCamelCase.

@chanhdatng
Copy link

This doesn't work on all screen sizes. On bigger screens, it doesn't extend to the end of the border

Yeah, did you know how to solve it?

@nadjem
Copy link

nadjem commented Mar 25, 2021

Also works for NSView. Only need to swap top and bottom, since the coordinate system origin on macOS is in the left bottom of a view.

And per Swift naming guidelines, case names should be lowerCamelCase.

Great job. need declare CALayer for using with NSView
`

     let layer = CALayer();

    self.myView.layer = layer;

    self.myView.wantsLayer = true

`

@parveenkumar5
Copy link

what if i want only 2 rounded corner (top left and top right ) with border on three sides(top , left and right)

@mpkupriyanov
Copy link

@parveenkumar5 You can rewrite with OptionSet instead of Enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment