Last active
March 25, 2021 08:05
-
-
Save ruan65/df37843e70b591950b0c64c33bb074e6 to your computer and use it in GitHub Desktop.
Swift. Useful UIView extension for adding constraints with format
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
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