Home

Awesome

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:

<img src='https://github.com/lionheart/SuperLayout/raw/master/old1.png' width='589px' />

Into this:

<img src='https://github.com/lionheart/SuperLayout/raw/master/new1.png' width='355px' />

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

<details> <summary>Original</summary>
viewA.rightAnchor.constraint(equalTo: viewB.leftAnchor).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.rightAnchor.constraint(equalTo: viewB.leftAnchor, constant: C).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.rightAnchor.constraint(greaterThanOrEqualTo: viewB.leftAnchor).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.rightAnchor.constraint(greaterThanOrEqualTo: viewB.leftAnchor, constant: C).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.rightAnchor.constraint(lessThanOrEqualTo: viewB.leftAnchor).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.rightAnchor.constraint(lessThanOrEqualTo: viewB.leftAnchor, constant: C).isActive = true
</details>

NSLayoutDimension Reference

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

<details> <summary>Original</summary>
viewA.heightAnchor.constraint(equalTo: viewB.heightAnchor, multiplier: M).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.heightAnchor.constraint(equalTo: viewB.heightAnchor, multiplier: M, constant: C).isActive = true
</details>

constraint(equalToConstant:)viewA.heightAnchor ~~ C

<details> <summary>Original</summary>
viewA.heightAnchor.constraint(equalToConstant: C).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.heightAnchor.constraint(greaterThanOrEqualTo: viewB.heightAnchor, multiplier: M).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.heightAnchor.constraint(greaterThanOrEqualTo: viewB.heightAnchor, multiplier: M, constant: C).isActive = true
</details>

constraint(greaterThanOrEqualToConstant:)viewA.heightAnchor ≥≥ C

<details> <summary>Original</summary>
viewA.heightAnchor.constraint(greaterThanOrEqualToConstant: C).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.heightAnchor.constraint(lessThanOrEqualTo: viewB.heightAnchor, multiplier: M).isActive = true
</details>

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

<details> <summary>Original</summary>
viewA.heightAnchor.constraint(lessThanOrEqualTo: viewB.heightAnchor, multiplier: M, constant: C).isActive = true
</details>

constraint(lessThanOrEqualToConstant:)viewA.heightAnchor ≤≤ C

<details> <summary>Original</summary>
viewA.heightAnchor.constraint(lessThanOrEqualToConstant: C).isActive = true
</details>

License

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