A fluent API for constraint-based iOS layout.
A wrapper around the NSLayoutAnchor system, driven by the following set of design goals / trade-offs:
Ease of expression should be proportional to frequency of use. In other words: constraining a view to its superview’s edges should be trivial, and indeed it is: view.anchor()
Intuitiveness at the point of use. Constraining a view’s top to the bottom of another view should probably be expressed as: view.anchor(.top, to: .bottom, of: anotherView), and indeed it is.
Chainability to promote a little more declarative layout code. Constraining a view at its declaration point should be as easy as let view = UIView().anchoring(.width, to: 100), and indeed it is.
Proactiveness with the view hierarchy. When constraining anchors to match another view, such as this: UIActivityIndicatorView().anchor(.centerX, .centerY, to: view), it could be assumed that the receiver, if it is without superview, should be added as subview to the other view, and indeed it is.
// All edges
view.anchor()
// Some edges
view.anchor(.top, .left, .right)
// To safe area
view.anchor(to: .safeArea)
// With insets
view.anchor(with: UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20))
// Save constraint for later use
let constraint = view.anchor(.top)
// Inactive constraint
let constraint = view.anchor(.top, isActive: false)
// Prioritized like a complete refactoring in your backlog
view.anchor(.top, priority: .defaultLow)
// All edges
view.anchor(to: otherView)
// Some edges
view.anchor(.top, .left, .right, to: otherView)
// To safe area
view.anchor(to: .safeArea, of: otherView)
// With insets
view.anchor(to: otherView, with: UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20))
// Save constraint for later use
let constraint = view.anchor(.top, to: otherView)
// Inactive constraint
let constraint = view.anchor(.top, to: otherView, isActive: false)
// With priority
view.anchor(.top, to: otherView, priority: .defaultLow)
view.anchor(.width, to: 100)
view.anchor(.height, to: 100)
// or simply
view.anchor(.size, to: 100)
// Top to bottom of other view
view.anchor(.top, to: .bottom, of: otherView)
// Left greater than or equal to right of other view
view.anchor(.left, .greaterThanOrEqual, to: .right, of: otherView)
iOS 11
XCode 10
Swift 4.2
To integrate AnchorChain to your XCode project using CocoaPods,
add the following to your Podfile:
pod 'AnchorChain'
AnchorChain is available under the MIT license. See the LICENSE file for more info.