Skip to content

Instantly share code, notes, and snippets.

@konnnn
Created April 17, 2020 17:01
Show Gist options
  • Save konnnn/cbdb7f1a259ca3b346b0ba6d24a5cd13 to your computer and use it in GitHub Desktop.
Save konnnn/cbdb7f1a259ca3b346b0ba6d24a5cd13 to your computer and use it in GitHub Desktop.
Adds a border to the UIButton
// Created by Evgeny Konkin on 17/04/2020.
import UIKit
enum BorderSide {
case top, bottom, left, right
}
extension UIButton {
/// Добавляем границу кнопки
func addBorder(side: BorderSide, color: UIColor, thickness: CGFloat) {
let border = CALayer()
border.backgroundColor = color.cgColor
switch side {
case .top:
border.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: thickness)
case .bottom:
border.frame = CGRect(x: 0, y: frame.size.height - thickness, width: frame.size.width, height: thickness)
case .left:
border.frame = CGRect(x: 0, y: 0, width: thickness, height: frame.size.height)
case .right:
border.frame = CGRect(x: frame.size.width, y: 0, width: thickness, height: frame.size.height)
}
self.layer.addSublayer(border)
}
}
// Usage
let button = UIButton()
button.addBorder(side: .bottom, color: .gray, thickness: 0.5)
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment