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)
}
}
@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