Skip to content

Instantly share code, notes, and snippets.

@ruan65
Last active March 25, 2021 08:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruan65/df37843e70b591950b0c64c33bb074e6 to your computer and use it in GitHub Desktop.
Save ruan65/df37843e70b591950b0c64c33bb074e6 to your computer and use it in GitHub Desktop.
Swift. Useful UIView extension for adding constraints with format
extension UIView {
func addConstraintsWithFormat(format: String, views: UIView...) {
var viewsDict = [String: UIView]()
for (index, view) in views.enumerated() {
view.translatesAutoresizingMaskIntoConstraints = false
viewsDict["v\(index)"] = view
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDict))
}
func addConstraintsFillEntireView(view: UIView) {
addConstraintsWithFormat(format: "H:|[v0]|", views: view)
addConstraintsWithFormat(format: "V:|[v0]|", views: view)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment