KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift
KVConstraintKit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "KVConstraintKit"The following types of operators are provided by KVConstraintKit, to add, remove, access and modify constraints.
| Operator | Meaning |
|---|---|
| + | to add constraint |
| - | to remove constraint |
| * | to modify multiplier of constraint |
| ~ | to modify Priority ( UILayoutPriority ), Relation ( NSLayoutRelation ) & Replace constraint |
| <- | to access constraint by attributes ( eg. NSLayoutAttribute.Height ) |
| +== | to add equal relation ( NSLayoutRelation.Equal ) constraint |
| +>= | to add greater than or equal relation ( NSLayoutRelation.GreaterThanOrEqual ) constraint |
| +<= | to add less than or equal relation ( NSLayoutRelation.LessThanOrEqual ) constraint |
| *== | to add equal relation ( NSLayoutRelation.Equal ) constraint with multiplier |
| *>= | to add greater than or equal relation ( NSLayoutRelation.GreaterThanOrEqual ) constraint with multiplier |
| *<= | to add less than or equal relation ( NSLayoutRelation.LessThanOrEqual ) constraint with multiplier |
Here's a quick example:
let v = UIView.prepareNewViewForAutoLayout()
v.backgroundColor = UIColor.redColor()
view.addSubview(v)
// Prepare constraints and then add them on superview of view
let top = NSLayoutConstraint( item: v, attribute: .Top,
relatedBy: .Equal,
toItem: v.superview!, attribute: .Top,
multiplier: 1.0, constant: 0)
let leading = NSLayoutConstraint( item: v, attribute: .Leading,
relatedBy: .Equal,
toItem: v.superview!, attribute: .Leading,
multiplier: 1.0, constant: 0)
let trailing = NSLayoutConstraint( item: v, attribute: .Trailing,
relatedBy: .Equal,
toItem: v.superview!, attribute: .Trailing,
multiplier: 1.0, constant: 0)
let bottom = NSLayoutConstraint( item: v, attribute: .Bottom,
relatedBy: .Equal,
toItem: v.superview!, attribute: .Bottom,
multiplier: 1.0, constant: 0)
v.superview?.addConstraints([top, leading, trailing, bottom])v +== [.Top, .Leading, .Trailing, .Bottom]
##### Similarly for margin constraints
> ```swift
v +== [ .LeadingMargin, .TrailingMargin, .TopMargin, .BottomMargin]
Horizontally
view.applyConstraintFitHorizontallyToSuperview()
OR
view.applyConstraintFitHorizontallyToSuperview(20) // paddingVertically
view.applyConstraintFitVerticallyToSuperview()
OR
view.applyConstraintFitVerticallyToSuperview(20) // paddingHorizontally & Vertically
view.applyConstraintFitToSuperview()
OR
view.applyConstraintFitToSuperview(20) // width same padding for all edgeFit with inset
let inset = UIEdgeInsets(top: 4, left: 8, bottom: 12, right: 16)
view.applyConstraintFitToSuperview(contentInset:inset)Width
view.applyWidthConstraint(100)
OR
view.applyAtLeastWidthConstraint(100)
OR
view.applyAtMostWidthConstraint(100)Height
view.applyHeightConstraint(100)
OR
view.applyAtLeastHeightConstraint(100)
OR
view.applyAtMostHeightConstraint(100)Aspact Ratio
view.applyAspectRatioConstraint()KVConstraintKit supports all built-in layout attributes as of iOS, tvOS & OSX, see the NSLayoutAttribute enum.
For more details see the ApplyViewConstraint extension and LayoutRelationable Protocol of KVConstraintKit.
Note: Avoid using Left and Right attributes. Use Leading and Trailing instead. This allows the layout to adapt to the view’s reading direction. By default the reading direction is determined based on the current language set by the user.
KVConstraintKit is available under the MIT license. See the LICENSE file for more info.
Any contribution is more than welcome! You can contribute through pull requests and issues on GitHub.
If you wish to contact me, email at: keshavvbe@gmail.com








