Skip to content

Instantly share code, notes, and snippets.

@gabrieltheodoropoulos
Last active October 16, 2019 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrieltheodoropoulos/394d07af043b6890f61d471009d53de0 to your computer and use it in GitHub Desktop.
Save gabrieltheodoropoulos/394d07af043b6890f61d471009d53de0 to your computer and use it in GitHub Desktop.
iOS/Swift - A UIView extension to easily add borders on any side of any view.
/**
This is a UIView extension.
- Borders are actually UIView objects which are added as subviews to self.
- Border views are layed out to self using constraints so they'll work on any change of the view.
- Use `BorderSide` enum values to specify the sides of the view you want to add borders to.
- For each `BorderSide` value pass along the desired thickness and color of the border as associated values.
Example:
testView.addBorder(on: [.bottom(thickness: 1.0, color: .blue), .left(thickness: 2.0, color: .yellow)])
*/
extension UIView {
enum BorderSide {
case top(thickness: CGFloat = 1.0, color: UIColor = UIColor.lightGray)
case bottom(thickness: CGFloat = 1.0, color: UIColor = UIColor.lightGray)
case right(thickness: CGFloat = 1.0, color: UIColor = UIColor.lightGray)
case left(thickness: CGFloat = 1.0, color: UIColor = UIColor.lightGray)
}
func addBorder(on sides: [BorderSide]) {
for side in sides {
let border = UIView(frame: .zero)
border.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(border)
switch side {
case .top(let thickness, let color):
NSLayoutConstraint(item: border, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: thickness).isActive = true
border.backgroundColor = color
case .bottom(let thickness, let color):
NSLayoutConstraint(item: border, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: thickness).isActive = true
border.backgroundColor = color
case .left(let thickness, let color):
NSLayoutConstraint(item: border, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: thickness).isActive = true
border.backgroundColor = color
case .right(let thickness, let color):
NSLayoutConstraint(item: border, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: border, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: thickness).isActive = true
border.backgroundColor = color
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment