Skip to content

Instantly share code, notes, and snippets.

@morishin
Created September 2, 2017 02:36
Show Gist options
  • Save morishin/a40fd068a4479514897443d2cb1a2417 to your computer and use it in GitHub Desktop.
Save morishin/a40fd068a4479514897443d2cb1a2417 to your computer and use it in GitHub Desktop.
import UIKit
extension UIView {
func addBorderViews(top: Bool = false, left: Bool = false, bottom: Bool = false, right: Bool = false, color: UIColor = .black, width: CGFloat = 1.0) {
if top {
let borderView = UIView()
borderView.translatesAutoresizingMaskIntoConstraints = false
addSubview(borderView)
borderView.topAnchor.constraint(equalTo: topAnchor).isActive = true
borderView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
borderView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
borderView.heightAnchor.constraint(equalToConstant: width).isActive = true
borderView.backgroundColor = color
}
if left {
let borderView = UIView()
borderView.translatesAutoresizingMaskIntoConstraints = false
addSubview(borderView)
borderView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
borderView.topAnchor.constraint(equalTo: topAnchor).isActive = true
borderView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
borderView.widthAnchor.constraint(equalToConstant: width).isActive = true
borderView.backgroundColor = color
}
if bottom {
let borderView = UIView()
borderView.translatesAutoresizingMaskIntoConstraints = false
addSubview(borderView)
borderView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
borderView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
borderView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
borderView.heightAnchor.constraint(equalToConstant: width).isActive = true
borderView.backgroundColor = color
}
if right {
let borderView = UIView()
borderView.translatesAutoresizingMaskIntoConstraints = false
addSubview(borderView)
borderView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
borderView.topAnchor.constraint(equalTo: topAnchor).isActive = true
borderView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
borderView.widthAnchor.constraint(equalToConstant: width).isActive = true
borderView.backgroundColor = color
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment