README.md

Anchorage

A lightweight collection of intuitive operators and utilities that simplify Auto Layout code. Anchorage is built directly on top of the NSLayoutAnchor API.

Each expression acts on one or more NSLayoutAnchors, and returns active NSLayoutConstraints. If you want inactive constraints, here's how to do that.

Usage

Alignment

// Pin the button to 12 pt from the leading edge of its containerbutton.leadingAnchor== container.leadingAnchor+12// Pin the button to at least 12 pt from the trailing edge of its containerbutton.trailingAnchor<= container.trailingAnchor-12// Center one or both axes of a viewbutton.centerXAnchor== container.centerXAnchor
button.centerAnchors== container.centerAnchors

Relative Alignment

// Position a view to be centered at 2/3 of its container's widthview.centerXAnchor==2* container.trailingAnchor/3// Pin the top of a view at 25% of container's heightview.topAnchor== container.bottomAnchor/4

Sizing

// Constrain a view's width to be at most 100 ptview.widthAnchor<=100// Constraint a view to a fixed sizeimageView.sizeAnchors==CGSize(width: 100, height: 200)
// Constrain two views to be the same sizeimageView.sizeAnchors== view.sizeAnchors// Constrain view to 4:3 aspect ratioview.widthAnchor==4* view.heightAnchor/3

Composite Anchors

Constrain multiple edges at a time with this syntax:

// Constrain the leading, trailing, top and bottom edges to be equalimageView.edgeAnchors== container.edgeAnchors// Inset the edges of a view from another viewlet insets =UIEdgeInsets(top: 5, left: 10, bottom: 15, right: 20)
imageView.edgeAnchors== container.edgeAnchors+ insets
// Inset the leading and trailing anchors by 10imageView.horizontalAnchors>= container.horizontalAnchors+10// Inset the top and bottom anchors by 10imageView.verticalAnchors>= container.verticalAnchors+10

Use leading and trailing

Using leftAnchor and rightAnchor is rarely the right choice. To encourage this, horizontalAnchors and edgeAnchors use the leadingAnchor and trailingAnchor layout anchors.

Inset instead of Shift

When constraining leading/trailing or top/bottom, it is far more common to work in terms of an inset from the edges instead of shifting both edges in the same direction. When building the expression, Anchorage will flip the relationship and invert the constant in the constraint on the far side of the axis. This makes the expressions much more natural to work with.

Priority

The ~ is used to specify priority of the constraint resulting from any Anchorage expression:

// Align view 20 points from the center of its superview, with system-defined low priorityview.centerXAnchor== view.superview.centerXAnchor+20~ .low// Align view 20 points from the center of its superview, with (required - 1) priorityview.centerXAnchor== view.superview.centerXAnchor+20~ .required-1// Align view 20 points from the center of its superview, with custom priorityview.centerXAnchor== view.superview.centerXAnchor+20~752

The layout priority is an enum with the following values:

.required - UILayoutPriorityRequired (default)

.high - UILayoutPriorityDefaultHigh

.low - UILayoutPriorityDefaultLow

.fittingSize - UILayoutPriorityFittingSizeLevel

Storing Constraints

To store constraints created by Anchorage, simply assign the expression to a variable:

// A single (active) NSLayoutConstraintlet topConstraint = (imageView.topAnchor== container.topAnchor)
// EdgeConstraints represents a collection of constraints// You can retrieve the NSLayoutConstraints individually,// or get an [NSLayoutConstraint] via .all, .horizontal, or .verticallet edgeConstraints = (button.edgeAnchors== container.edgeAnchors).all

Batching Constraints

By default, Anchorage returns active layout constraints. If you'd rather return inactive constraints for use with the NSLayoutConstraint.activate(_:) method for performance reasons, you can do it like this:

You can also pass active: true if you want the constraints in the array to be automatically activated in a batch.

Autoresizing Mask

Anchorage sets the translatesAutoresizingMaskIntoConstraints property to false on the left hand side of the expression, so you should never need to set this property manually. This is important to be aware of in case the container view relies on translatesAutoresizingMaskIntoConstraints being set to true. We tend to keep child views on the left hand side of the expression to avoid this problem, especially when constraining to a system-supplied view.

Installation

CocoaPods

To integrate Anchorage into your Xcode project using CocoaPods, specify it in
your Podfile:

pod 'Anchorage'

Carthage

To integrate Anchorage into your Xcode project using Carthage, specify it in
your Cartfile:

github "Raizlabs/Anchorage"

Run carthage update to build the framework and drag the built
Anchorage.framework into your Xcode project.

License

This code and tool is under the MIT License. See LICENSE file in this repository.