-
-
Save anelad/5bcff9dd1a7a4003b7f5e4135fdbf28a to your computer and use it in GitHub Desktop.
Swift 4 Extension for adding border to one side of UIView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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