Skip to content

Instantly share code, notes, and snippets.

@MrJackdaw
Last active January 21, 2019 12:40
Show Gist options
  • Save MrJackdaw/b807f61dae54dbf2487c36a2f69bece7 to your computer and use it in GitHub Desktop.
Save MrJackdaw/b807f61dae54dbf2487c36a2f69bece7 to your computer and use it in GitHub Desktop.
Swift 2 Extension for adding a border to one side of a UIView object
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 = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), thickness, CGRectGetHeight(frame))
break
case .Right: border.frame = CGRectMake(CGRectGetMaxX(frame), CGRectGetMinY(frame), thickness, CGRectGetHeight(frame))
break
case .Top: border.frame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), CGRectGetWidth(frame), thickness)
break
case .Bottom: border.frame = CGRectMake(CGRectGetMinX(frame), CGRectGetMaxY(frame), CGRectGetWidth(frame), thickness)
break
}
layer.addSublayer(border)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment