Skip to content

Instantly share code, notes, and snippets.

@buoge
Forked from Isuru-Nanayakkara/UIButton+Border.swift
Created November 5, 2015 11:46
Show Gist options
  • Save buoge/fb02fc98f597e0878809 to your computer and use it in GitHub Desktop.
Save buoge/fb02fc98f597e0878809 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(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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment