Skip to content

Imperiopolis/Swiftly

Repository files navigation

Swiftly

Version License Platform

Swiftly generate Auto Layout constraints.

Usage

To run the example project, simply run pod try swiftly. Alternatively, you can clone the repo and run the project in the example directory.

All UIViews and UILayoutGuides respond to the applyLayout method which takes a variadic list of Swiftly objects. Convenience initializers are available which pair with all of Apple's NSLayoutAttribute types. Common combinatorial layout types flush, flushToMargins, vertical, horizontal, center, and size are also available.

view.applyLayout(.centerX, .vertical, .width * 0.5)

Installation

Cocoapods

Swiftly is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Swiftly"

Carthage

Swiftly is available through Carthage. To install it, simply add the following line to your Cartfile:

github "Imperiopolis/Swiftly" ~> 2.0

Swift Version

Swiftly 2.0 and later require Swift 4.2. For older versions of Swift, please use the Swiftly 1.0 build.

Custom Operators

Operators can be used on Swiftly objects to produce modified layouts. The ==, <=, >=, +, -, *, ~=, and / operators are available.

view.applyLayout(.centerX, .top + 20, .width * 0.5, .height == 200)

Setting Priority

The priority of Swiftly objects may be configured.

view.applyLayout(.centerY ~= UILayoutPriorityRequired)

View Relationships

By default, layout types reference the views superview. To create a constraint relative to a sibling view pass that view as a paramter.

view1.applyLayout(.left == .right(view2) + 5, .size(view2))

Constraint Manipulation

The applyLayout method returns an array of the generated LayoutConstraint objects, which can be used to later to easily disable or modify the generated constraints.

let constraints = view.applyLayout(.size == 5, .center)
NSLayoutConstraint.deactivate(constraints)

...

NSLayoutConstraint.activate(constraints)

Note: Any view which has applyLayout called on it will automatically set translatesAutoresizingMaskIntoConstraints to false and have the generated constraints added to its superview.

Documentation

Read the documentation here.

About Swiftly

Swiftly was created by @Imperiopolis and was intended as a lightweight version of Cartography by Robb Böhnke.

Swiftly is released under the MIT license. See LICENSE for details.