Skip to content
Intuitive and powerful Auto Layout library
Swift Ruby
Branch: master
Clone or download
Latest commit 6611541 Jun 21, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Align.playground Rebrand Jun 20, 2019
Align.xcodeproj Version 1.2 Jun 20, 2019
Docs Rebrand Jun 20, 2019
Sources Rebrand Jun 20, 2019
Supporting Rebrand Jun 20, 2019
Tests Rebrand Jun 20, 2019
.gitignore Initial commit Nov 7, 2017
.travis.yml Rebrand Jun 20, 2019
Align.podspec Rebrand Jun 20, 2019
CHANGELOG.md Version 1.2 Jun 20, 2019
LICENSE Initial commit Nov 7, 2017
Package.swift Rebrand Jun 20, 2019
README.md

README.md

Align

An intuitive and powerful Auto Layout library.

The entire library fits in a single file with less than 300 lines of code which you can just drag-n-drop into your app.

Align strives for clarity and simplicity by following Swift API Design Guidelines. Although most of the APIs are compact, it is a non-goal to enable the most concise syntax possible. Instead, Align has a fluent API that makes use sites form grammatical English phrases - that's what makes Swift code really stand out.

Anchors

Start by selecting an anchor or a collection of anchors of a view (or of a layout guide). Then use anchor's methods to create constraints.

Anchors represent layout attributes of a view including edges, dimensions, axis, and baselines.

Each anchor and collection of anchors have methods tailored for that particular kind of anchor:

Constraints(for: title, subtitle) { title, subtitle in
    // Align one anchor with another
    subtitle.top.align(with: title.bottom + 10)

    // Align center with a superview
    title.centerX.alignWithSuperview()

    // Manipulate dimenstions
    title.width.set(100)
    subtitle.width.match(title.width * 2)

    // Change a priority of constraints inside a group:
    subtitle.bottom.pinToSuperview().priority = UILayoutPriority(999)
}

All anchors can be also be accessed using .al proxy:

title.al.top.pinToSuperview()

The best way to access anchors is by using a special addSubview(_:constraints:) method (supports up to 4 views). Here are some examples of what you can do using anchors:

view.addSubview(subview) {
    $0.edges.pinToSuperview(insets: Insets(10)) // Pin to superview edges
}
view.addSubview(subview) {
    $0.edges.pinToSuperviewMargins() // Or margins
}
view.addSubview(subview) {
    $0.edges(.left, .right).pinToSuperview() // Fill along horizontal axis
    $0.centerY.alignWithSuperview() // Center along vertical axis
}

With addSubview(_:constraints:) method you define a view hierarchy and layout views at the same time. It encourages splitting layout code into logical blocks and prevents some programmer errors (e.g. trying to add constraints to views which are not in a view hierarchy).

Align has full test coverage. If you'd like to learn about which constraints (NSLayoutConstraint) Align creates each time you call one of its methods, test cases are a great place to start.

Requirements

Align Swift Xcode Platforms
Align 1.1-1.2 Swift 4.2 – 5.0 Xcode 10.1 – 10.2 iOS 10.0 / tvOS 10.0
Align 1.0 Swift 4.0 – 4.2 Xcode 9.2 – 10.1 iOS 9.0 / tvOS 9.0

Why Align

Align is for someone who:

You can’t perform that action at this time.