Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BrettRToomey/728c6f68ffbae47aa0b1638229e7367e to your computer and use it in GitHub Desktop.
Save BrettRToomey/728c6f68ffbae47aa0b1638229e7367e to your computer and use it in GitHub Desktop.
Add a border to a UIButton. Original code - http://stackoverflow.com/a/21881788/1077789
import Foundation
import UIKit
public enum UIButtonBorderSide {
case Top, Bottom, Left, Right
}
extension UIButton {
public func addBorder(sides: [UIButtonBorderSide], color: UIColor, width: CGFloat) {
let border = CALayer()
border.backgroundColor = color.CGColor
for side in sides
{
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)
}
}
}
@NameX44
Copy link

NameX44 commented May 5, 2017

Hi,

You have to move :

let border = CALayer() border.backgroundColor = color.CGColor

into for loop to make it works properly on iOS 10.3 (didn't test your code in another version).

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment