Skip to content

Instantly share code, notes, and snippets.

@anelad
Forked from MrJackdaw/UIView+Border.swift
Last active March 5, 2018 12:56
Show Gist options
  • Save anelad/5bcff9dd1a7a4003b7f5e4135fdbf28a to your computer and use it in GitHub Desktop.
Save anelad/5bcff9dd1a7a4003b7f5e4135fdbf28a to your computer and use it in GitHub Desktop.
Swift 4 Extension for adding border to one side of UIView
// Ready for Swift 4
extension UIView {
// Example usage: myView.addBorder(toSide: .left, withColor: .red, andThickness: 1.0)
enum Side {
case left, right, top, bottom
}
func addBorder(toSide side: Side, withColor color: UIColor, andThickness thickness: CGFloat) {
let border = CALayer()
border.backgroundColor = color.cgColor
switch side {
case .left: border.frame = CGRect(x: bounds.minX, y: bounds.minY, width: thickness, height: bounds.height); break
case .right: border.frame = CGRect(x: bounds.maxX, y: bounds.minY, width: thickness, height: bounds.height); break
case .top: border.frame = CGRect(x: bounds.minX, y: bounds.minY, width: bounds.width, height: thickness); break
case .bottom: border.frame = CGRect(x: bounds.minX, y: bounds.maxY, width: bounds.width, height: thickness); break
}
print(border.frame)
layer.addSublayer(border)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment