Novoda Constraints is a lightweight, pure-Swift library designed to assist developers in setting up and creating constraints programmatically.
- Align multiple views by specified edges
- Stack multiple views with direction + spacing
- Set size via constants / size equal to view
- Pin views to eachother
- Pin views to superview / safe area
- Optionally set constraint attributes such as priority, inset and relation type
Writing constraints using the default iOS constraints can be cumbersome and lead to large copy and pasted code. Here is an example of aligning two views using iOS constraints:
let leadingAlign = NSLayoutConstraint(item: view,
attribute: .leading,
relatedBy: .equal,
toItem: otherView,
attribute: .leading,
multiplier: 1,
constant: 0)
let trailingAlign = NSLayoutConstraint(item: view,
attribute: .trailing,
relatedBy: .equal,
toItem: otherView,
attribute: .trailing,
multiplier: 1,
constant: 0)
view.addConstraint(leadingAlign)
view.addConstraint(trailingAlign)
And here is an example of aligning two views using Novoda Constraints:
view.align(.leading, .trailing,
with: otherView)
You can optionally add extra parameters to most of the functions available in this library such as:
view.align(.leading, .trailing,
with: otherView,
constant: 0,
priority: .required,
relatedBy: .equal)
If you want to align multiple views you can do this in one line!
viewArray.align(edges: .leading, .trailing)
You can also stack views in combination with the above to quickly create entire screens:
viewArray.stack(direction: .vertical,
spacing: 30, 8)
There are full details on how the
spacing
parameter works on the function docs in the library itself.
In short; the spacing parameter takes any number of spacings and applies these spacings to the views, the last spacing provided will be used for any subsequent views. E.g. provided a single spacing of 8 will apply 8 spacing to every view in the array.
You can size the height and width of an array of views to the same value using .size:
viewArray.size(height: 50)
or
viewArray.size(width: 50)
You can pin a view to its superview, its superview safe area, its center axis or to another view
view.pin(toSuperview: .leading, .trailing, insetBy: 30)
view.pin(toSuperview: Edge.all)
view.pin(toSuperviewSafeArea: .leading, .trailing, insetBy: 30)
view.pin(toSuperviewSafeArea: Anchor.all)
view.pin(centerXTo: otherView)
view.pin(centerYTo: otherView)
view.pin(centerTo: otherView)
view.pin(.top, to: .bottom, of: otherView)
view.pin(.top, to: .bottom, of: otherView, constant: 30)
Note how when pinning to superview we use Edge.all and for superview safe area we use Anchor.all. This is due to the fact that the safe area layout guide uses anchors instead of constraint attributes.
You can set the size constraints of a view using direct values or by setting it equal to another view
view.set(height: 30)
view.set(width: 100)
view.set(size: CGSize(width: 100,
height: 30))
view.set(heightEqualTo: otherView)
view.set(widthEqualTo: otherView)
view.set(sizeEqualTo: otherView)
As you can see, the usage is much shorter and cleaner, allowing you to focus on the important things.
To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS 9.0+ Swift 4.0+
novoda-constraints is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'novoda-constraints'
Novoda iOS Team
novoda-constraints is available under the Apache license. See the LICENSE file for more info.