Swiftly generate Auto Layout constraints.
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 UIView
s and UILayoutGuide
s 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)
Swiftly is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Swiftly"
Swiftly is available through Carthage. To install it, simply add the following line to your Cartfile:
github "Imperiopolis/Swiftly" ~> 2.0
Swiftly 2.0 and later require Swift 4.2. For older versions of Swift, please use the Swiftly 1.0 build.
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)
The priority of Swiftly
objects may be configured.
view.applyLayout(.centerY ~= UILayoutPriorityRequired)
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))
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.
Read the documentation here.
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.