README.md

EasyPeasy is a Swift framework that lets you create Auto Layout constraints
programmatically without headaches and never ending boilerplate code. Besides the
basics, EasyPeasy resolves most of the constraint conflicts for you and also
can attach to a constraint conditional closures that are evaluated before applying
a constraint, this way you can install an Auto Layout constraint depending on
platform, size classes, orientation... or the state of your controller, easy peasy!

In this quick tour through EasyPeasy we assume that you already know the
advantages and disadvantages of the different Auto Layout APIs and therefore you
won't see here a comparison of the code side by side, just read and decide
whether EasyPeasy is for you or not.

A touch of EasyPeasy

The example below is quite simple but shows how effortless its implementation
result using EasyPeasy.

Installation

Swift compatibility

To work with Swift 2.2 use EasyPeasy v.1.2.1 or earlier versions of the library.

To work with Swift 2.3 use EasyPeasy v.1.3.1.

To work with Swift 3 use EasyPeasy v.1.4.2.

To work with Swift 4 use EasyPeasy v.1.5.0 and above
(thanks Bas van Kuijck).

Cocoapods

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

pod "EasyPeasy"

Carthage

EasyPeasy is Carthage compatible.
To add EasyPeasy as a dependency to your project, just add the following line
to your Cartfile:

github "nakiostudio/EasyPeasy"

And run carthage update as usual.

Compatibility

EasyPeasy is compatible with iOS (8 and above), tvOS (9 and above) and OS X
(10.10 and above).
The framework has been tested with Xcode 7 and Swift 2.0, however don't hesitate
to report any issues you may find with different versions.

Usage

EasyPeasy is a set of position and dimension attributes that you can apply
to your views. You can manage these from the easy property available within all
the UI classes that work with Auto Layout (view subclasses, layout guides, etc).

For instance, to set a width of 200px to a view you would create
an attribute of class Width with a constant value of 200, then the attribute
is applied to the view by using the easy.layout(_:) method.

myView.easy.layout(Width(200))

Because our view without height is nothing we can apply multiple attributes at
once as follows:

myView.easy.layout(
Width(200),
Height(120)
)

In the previous example, two attributes have been applied and therefore two constraints
created and added: a width constraint with constant = 200 and a height constraint
with constant = 120.

Constants

Without really knowing it, we have just created an EasyPeasyConstant struct
containing the constant, multipler and the relation of a NSLayoutConstraint.

Relations

EasyPeasy provides an easy way of creating constants with different
NSLayoutRelations:

.Equal: it is created like in our previous example Width(200).

.GreaterThanOrEqual: it is created as easy as this Width(>=200) and it means
that our view has a width greater than or equal to 200px.

.LessThanOrEqual: it is created as follows Width(<=200).

Multipliers

There is a custom operator that eases the creation of a NSLayoutConstraint multiplier.
You can use it like this Width(*2) and means that the width of our view is two times
something, we will mention later how to establish the relationship with that something.

Attributes

EasyPeasy provides as many Attribute classes as attributes NSLayoutConstraint
have, plus something that we have called CompoundAttributes (we will explain these
attributes later).

DimensionAttributes

There are just two dimension attributes Width and Height. You can create an
Auto Layout relationship between your view DimensionAttribute and another view
by using the method func like(view: UIView) -> Self. Example:

contentLabel.easy.layout(Width().like(headerView))

That line of code will create a constraint that sets a width for contentLabel
equal to the headerView width.

PositionAttributes

The table below shows the different position attributes available. Because they
behave like the NSLayoutConstraint attributes, you can find a complete
description of them in the Apple docs.

Attribute

Attribute

Attribute

Attribute

Left

Right

Top

Bottom

Leading

Trailing

CenterX

CenterY

LeftMargin

RightMargin

TopMargin

BottomMargin

LeadingMargin

TrailingMargin

CenterXWithinMargins

CenterYWithinMargins

FirstBaseline

LastBaseline

--

--

As well as the DimensionAttributes have the like: method to establish
Auto Layout relationships, you can use a similar method to do the same with
PositionAttributes. This method is:

functo(view: UIView, _attribute: ReferenceAttribute?=nil) ->Self

The example below positions contentLabel 10px under headerView with the same
left margin as headerView.

CompoundAttributes

These attributes are the ones that create multiple DimensionAttributes or
PositionAttributes under the hood. For example, the Size attribute will create
a Width and a Height attributes with their width and height
NSLayoutConstraints respectively.

These are the CompoundAttributes available:

Size: As mentioned before this attribute will apply a Width and a Height
attribute to the view. It can be initialized in many ways and depending on that
the result may change. These are some examples:

Priorities

The Priority enum does the same function as UILayoutPriority and it's shaped
by five cases:

low: it creates an Auto Layout priority with Float value 1.

medium: it creates an Auto Layout priority with Float value 500.

high: it creates an Auto Layout priority with Float value 750.

required: it creates an Auto Layout priority with Float value 1000.

custom: it specifies the Auto Layout priority defined by the
developer in the case associated value value. Example: .custom(value: 650.0).

In order to apply any of these priorities to an Attribute, the method
.with(priority: Priority) must be used. The following example gives an
UILayoutPriority of 500 to the TopAttribute applied to view:

view.easy.layout(Top(>=50).with(.medium))

You can also apply a Priority to an array of Attributes (this operation will
override the priorities previously applied to an Attribute).

view.easy.layout([
Width(200),
Height(200)
].with(.medium))

Conditions

One of the peculiarities of EasyPeasy is the usage of Conditions or closures
that evaluate whether a constraint should be applied or not to the view.

The method when(condition: Condition) sets the Condition closure to an Attribute.

There is plenty of use cases, the example below shows how to apply different
constraints depending on a custom variable:

ContextualConditions

This iOS only feature is a variant of the Condition closures that receive no
parameters and return a boolean value. Instead, a Context struct is passed
as parameter providing some extra information based on the UITraitCollection
of the UIView the Attributes are going to be applied to.

The properties available on this Context struct are:

isPad: true if the current device is iPad.

isPhone: true if the current device is iPhone.

isHorizontalVerticalCompact: true if both horizontal and vertical size
classes are .Compact.

isHorizontalCompact: true if the horizontal size class is .Compact.

isVerticalCompact: true if the vertical size class is .Compact.

isHorizontalVerticalRegular: true if both horizontal and vertical size
classes are .Regular.

isHorizontalRegular: true if the horizontal size class is .Regular.

isVerticalRegular: true if the vertical size class is .Regular.

This is an example of ContextualConditions applied to an array of
Attributes:

ContextualCondition re-evaluation

As we have seen before, you can re-evaluate a Condition closure by calling
the easy.reload() convenience method. This also applies to
ContextualConditions, therefore if you want your constraints to be updated
upon a change on your view UITraitCollection then you need to call the
easy.reload() method within traitCollectionDidChange(_:).

Alternatively, EasyPeasy can do this step for you automatically. This is
disabled by default as it requires method swizzling; to enable it simply
compile the framework adding the compiler flags -D EASY_RELOAD.

UILayoutGuides

Since the version v.0.2.3 (and for iOS 9 projects and above) EasyPeasy
integrates UILayoutGuides support.

Applying constraints

Applying a constraint to an UILayoutGuide is as easy as we have discussed in the
previous sections, just apply the EasyPeasy attributes you want using the
easy.layout(_:) method.

As you can see, all the different attributes and goodies EasyPeasy provides for
UIViews are also applicable to UILayoutGuides.

Connecting UILayoutGuides and UIViews

As mentioned in the Attributes section you can create constraint
relationships between an UIView attribute and other UIViews attributes using
the methods to(_:_) and like(_:_). Now you can take advantage of those methods
to create a relationship between your UIView attributes and an UILayoutGuide.

Lastly

Finally but not less important in this section we will explain how to interact
with Attributes once they have been applied to an UIView using the
easy.layout(_:) method.

Updating constraints

We briefly mentioned in the introductory section that EasyPeasy solves most
of the constraint conflicts and it's true. Usually, in order to update a constraint
or the constant of a constraint you have to keep a reference to your
NSLayoutConstraint and update the constant when needed. With EasyPeasy you
just need to apply another Attribute to your UIView of the same or different
type. In the example below we have two methods, the one in which we setup our
constraints viewDidLoad() and a method in which we want to update the Top
attribute of our headerView.

That's it! we have updated our Top constraint without caring about keeping
references or installing/uninstalling new constraints.

However, there is some cases in which EasyPeasy cannot prevent a conflict (at
least for now). This is when multiple constraints cannot be satisfied, i.e. existing
a Left and Right constraints it's also applied a Width constraint (all of them
with the same priority). But EasyPeasy is smart enough to prevent conflicts,
i.e. when replacing a Left and Right attributes with a CenterX attribute.

Clearing constraints

EasyPeasy provides a method extending UIView that clears all the constraints
installed in an UIView by the framework. This method is func easy.clear().

view.easy.clear()

Animating constraints

Animating constraints with EasyPeasy is very straightforward, just apply one
or more Attributes to your view within an animation block and you are ready to
go, without worrying about constraint conflicts. Example: