CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.
TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Nov 2016 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Daniela Dias, Shagun Madhikarmi.
UIView
extension to easily create common Auto Layout Constraints for iOS.
Using Auto Layout programatically (before iOS 9’s Auto Layout API) can either be quite verbose i.e. building NSLayoutConstraint
objects for each rule or error prone e.g. (using Visual Format Language strings)
We can make creating common NSLayoutConstraint
relations into some reusable methods we can call on any class that subclasses UIView
. We also ensure the constraint created gets added to the view’s superview for you.
This means you can relate a view to another view with the NSLayoutAttribute
or NSLayoutRelation
you need, as part of the view’s setup and also helps us keep the code DRY.
UIView+AutoLayoutHelper.swift
file to your Xcode project.Add NSLayoutConstraint
relations for a UIView
relating its top, leading, trailing and bottom edges to it’s superview
// Create view
let leftView = UIView()
leftView.backgroundColor = UIColor.red
view.addSubview(leftView)
// Add constraints
leftView.addTopConstraint(toView: superview, attribute: .top, relation: .equal, constant: 10.0)
leftView.addLeadingConstraint(toView: superview, attribute: .leading, relation: .equal, constant: 10.0)
leftView.addTrailingConstraint(toView: superview, attribute: .trailing, relation: .equal, constant: -10.0)
leftView.addBottomConstraint(toView: superview, attribute: .bottom, relation: .equal, constant: -10.0)
or shorter you can omit the attributes:
leftView.addTopConstraint(toView: superview, constant: 10.0)
leftView.addLeadingConstraint(toView: superview, constant: 10.0)
leftView.addTrailingConstraint(toView: superview, constant: -10.0)
leftView.addBottomConstraint(toView: superview, constant: -10.0)
or even shorter using fillSuperView
(via @danieladias )
let edgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
leftView.fillSuperView(edgeInsets)
Add constraints to center a UIView
within its superview both vertically (Y axis) and horizontally (X axis):
label.addCenterXConstraintToView(label.superview, relation: .equal, constant:0.0)
label.addCenterYConstraintToView(label.superview, relation: .equal, constant:0.0)
Add constraints for a fixed width and height amount:
view.addWidthConstraintWithRelation: .equal, constant:100.0)
view.addHeightConstraintWithRelation: .equal, constant:80.0)
Modify constraints (since the methods return them to you)
// Create a reference to the `NSLayoutConstraint` e.g. for height
heightConstraint = view.addHeightConstraint(toView: nil, constant: 80.0)
...
// Update the height constant
heightConstraint.constant = 30.0;
// Animate changes
UIView.animateWithDuration(0.3, animations: { () -> Void in
view.layoutIfNeeded()
})
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See the Code of Conduct file
AutoLayoutHelper is released under the MIT License. See License.