Created
September 2, 2017 02:36
-
-
Save morishin/a40fd068a4479514897443d2cb1a2417 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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