Last active
August 9, 2021 12:11
-
-
Save Isuru-Nanayakkara/496d5713e61125bddcf5 to your computer and use it in GitHub Desktop.
Add a border to a UIButton. Original code - http://stackoverflow.com/a/21881788/1077789
Swift 4 + we can add this extension to UIView so its available across more elements.
public enum UIButtonBorderSide {
case top, bottom, left, right
}
extension UIView {
public func addBorder(side: UIButtonBorderSide, color: UIColor, width: CGFloat) {
let border = CALayer()
border.backgroundColor = color.cgColor
switch side {
case .top:
border.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: width)
case .bottom:
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
case .left:
border.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
case .right:
border.frame = CGRect(x: self.frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
}
self.layer.addSublayer(border)
}
}
lmL
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see my answer here: www.stackoverflow.com/a/40222533/2594699