Auto Layout has gotten nice, but it can still be pretty verbose. Cully provides handy lil’ helpers to reduce the amount of boilerplate code needed to layout views and dramatically improves the readability of that code.
Let’s say we want to position a fixed-size UIView in the center of another UIView.
Yes, one could use NSLayoutAnchors, and yes, perhaps when the world is all SwiftUI this becomes less relevant, and yes, _____________________, but this is the house I built and I intend on living in it. 🏡
Usage
import Cully
By default, Cully sets the receiver view’s translatesAutoresizingMaskIntoConstraints to false and activates the requested constraint.
Adding Constraints
Methods for adding constraints are namespaced constrain(...) on UIView.
Single constraint method names mirror NSLayoutContraint.Attributes. They all return a single NSLayoutConstraint. Some special case convenience helpers that require creating more than one constraint return [NSLayoutConstraint].
// Singly-activated `NSLayoutConstraint`s
let leftConstraint = myView.constrain(left: .equal, .left, of: someView)
let baselineConstraint = myView.constrain(lastBaseline: .lessThanOrEqual, .lastBaseline, of: someView)
// An array of `NSLayoutConstraint`s
let dimensionConstraints = myView.constrain(width: 50, height: 80)
Methods for accessing and modifying NSLayoutConstraints are namespaced constraint(...) and constraints(...) on UIView.
Cully is opinionatedly tailored to my own needs and uses, but feel free to open a pull request if you think of something cool, notice an issue, or feel something’s missing.
🏡 Cully
A Swift-based DSL to make Auto Layout friendlier.
Motivation
Auto Layout has gotten nice, but it can still be pretty verbose. Cully provides handy lil’ helpers to reduce the amount of boilerplate code needed to layout views and dramatically improves the readability of that code.
Let’s say we want to position a fixed-size
UIView
in the center of anotherUIView
.Woof – that’s a lot to parse. The same layout with Cully:
That’s friendlier.
NSLayoutConstraint
) and retains the full-power of the native APIYes, one could use
NSLayoutAnchors
, and yes, perhaps when the world is all SwiftUI this becomes less relevant, and yes, _____________________, but this is the house I built and I intend on living in it. 🏡Usage
By default, Cully sets the receiver view’s
translatesAutoresizingMaskIntoConstraints
tofalse
and activates the requested constraint.Adding Constraints
Methods for adding constraints are namespaced
constrain(...)
onUIView
.Single constraint method names mirror
NSLayoutContraint.Attribute
s. They all return a singleNSLayoutConstraint
. Some special case convenience helpers that require creating more than one constraint return[NSLayoutConstraint]
.Methods for accessing and modifying
NSLayoutConstraints
are namespacedconstraint(...)
andconstraints(...)
onUIView
.KeyedConstraints
is a typealias representing a Dictionary ofNSLayoutConstraint
s keyed byNSLayoutConstraint.Attribute
s onCollection
.You can also easily access the native
NSLayoutConstraint
initialization API, as well as a helper onUIView
to activate and deactivate constraints.Installation
Cully supports the Swift Package Manager in Xcode 11. Or give Carthage a try, how ‘bout?
Add the following to your
Cartfile
:Follow the Carthage instructions for adding a framework to your project.
Improvements
Cully is opinionatedly tailored to my own needs and uses, but feel free to open a pull request if you think of something cool, notice an issue, or feel something’s missing.