Skip to content

SuperLayout is a Swift library that makes using Auto Layout a breeze.

License

Notifications You must be signed in to change notification settings

lionheart/SuperLayout

Repository files navigation

CI Status Version License Platform

SuperLayout is a library that adds a few custom operators to Swift that makes using the amazing NSLayoutAnchor API for Auto Layout a breeze. SuperLayout doesn't override already-defined methods in Equatable (such as == and >=), and defines ones that are logical and easily understandable to anyone who might be inheriting your codebase or joining your team.

What It Does

In short, SuperLayout turns this:

Into this:

How it works

SuperLayout defines three custom operators: ~~, ≥≥, and ≤≤ that correspond to equalTo, to greaterThanOrEqualTo, and to lessThanOrEqualTo, respectively.

The greater than and less than operators were chosen with practicality in mind; and have simple keyboard shortcuts (just Option + < and Option + > in macOS), so there's no need to copy-paste characters when writing constraints.

Installation

SuperLayout is available via CocoaPods and SwiftPM. If you're using CocoaPods, just specify this in your Podfile:

pod 'SuperLayout'

Before You Use

To use this library, you should have a basic understanding of the NSLayoutAnchor API. If not, read up, and then check out the documentation below to get started.


NSLayoutConstraint Reference

Note: SuperLayout does not (yet) automatically turn off translatesAutoresizingMaskIntoConstraints for the views you'd like to manage with Auto Layout. Disabling this setting automatically is too magical for me. Feel free to create an issue if you disagree.

constraint(equalTo:)viewA.rightAnchor ~~ viewB.leftAnchor

Original
viewA.rightAnchor.constraint(equalTo: viewB.leftAnchor).isActive = true

constraint(equalTo:constant:)viewA.rightAnchor ~~ viewB.leftAnchor + C

Original
viewA.rightAnchor.constraint(equalTo: viewB.leftAnchor, constant: C).isActive = true

constraint(greaterThanOrEqualTo:)viewA.rightAnchor ≥≥ viewB.leftAnchor

Original
viewA.rightAnchor.constraint(greaterThanOrEqualTo: viewB.leftAnchor).isActive = true

constraint(greaterThanOrEqualTo:constant:)viewA.rightAnchor ≥≥ viewB.leftAnchor + C

Original
viewA.rightAnchor.constraint(greaterThanOrEqualTo: viewB.leftAnchor, constant: C).isActive = true

constraint(lessThanOrEqualTo:)viewA.rightAnchor ≤≤ viewB.leftAnchor

Original
viewA.rightAnchor.constraint(lessThanOrEqualTo: viewB.leftAnchor).isActive = true

constraint(lessThanOrEqualTo:constant:)viewA.rightAnchor ≤≤ viewB.leftAnchor + C

Original
viewA.rightAnchor.constraint(lessThanOrEqualTo: viewB.leftAnchor, constant: C).isActive = true

NSLayoutDimension Reference

constraint(equalTo:multiplier:)viewA.heightAnchor ~~ viewB.heightAnchor * M

Original
viewA.heightAnchor.constraint(equalTo: viewB.heightAnchor, multiplier: M).isActive = true

constraint(equalTo:multiplier:constant:)viewA.heightAnchor ~~ viewB.heightAnchor * M + C

Original
viewA.heightAnchor.constraint(equalTo: viewB.heightAnchor, multiplier: M, constant: C).isActive = true

constraint(equalToConstant:)viewA.heightAnchor ~~ C

Original
viewA.heightAnchor.constraint(equalToConstant: C).isActive = true

constraint(greaterThanOrEqualTo:multiplier:)viewA.heightAnchor ≥≥ viewB.heightAnchor * M

Original
viewA.heightAnchor.constraint(greaterThanOrEqualTo: viewB.heightAnchor, multiplier: M).isActive = true

constraint(greaterThanOrEqualTo:multiplier:constant:)viewA.heightAnchor ≥≥ viewB.heightAnchor * M + C

Original
viewA.heightAnchor.constraint(greaterThanOrEqualTo: viewB.heightAnchor, multiplier: M, constant: C).isActive = true

constraint(greaterThanOrEqualToConstant:)viewA.heightAnchor ≥≥ C

Original
viewA.heightAnchor.constraint(greaterThanOrEqualToConstant: C).isActive = true

constraint(lessThanOrEqualTo:multiplier:)viewA.heightAnchor ≤≤ viewB.heightAnchor * M

Original
viewA.heightAnchor.constraint(lessThanOrEqualTo: viewB.heightAnchor, multiplier: M).isActive = true

constraint(lessThanOrEqualTo:multiplier:constant:)viewA.heightAnchor ≤≤ viewB.heightAnchor * M + C

Original
viewA.heightAnchor.constraint(lessThanOrEqualTo: viewB.heightAnchor, multiplier: M, constant: C).isActive = true

constraint(lessThanOrEqualToConstant:)viewA.heightAnchor ≤≤ C

Original
viewA.heightAnchor.constraint(lessThanOrEqualToConstant: C).isActive = true

License

SuperLayout is available under the Apache 2.0 license. See LICENSE for more info.