Constrictor

Constrictor

Constrict your Auto Layout code with Constrictor, your chainable sugar.

Constrict (/ˈkənˈstrɪkt/), verb

“… to make something become tighter and narrower:”

Presentation

Build Status codecov CocoaPods Carthage compatible GitHub contributors apm

Features ✨

  • Compatible with Auto Layout 👍
  • Concise and chainable syntax ⛓️
  • Automatically sets translateAutoresizingMaskIntoConstraints 😍
  • Constraints are active by default 🤩
  • Easily update constraints 🏃
  • Allows setting priority upon creation 💁‍♂️

Usage Examples ⌨️

Simple

Constrictor allows you to fully replace NSLayoutAnchor. For example:

// NSLayoutAnchor
label.topAnchor.constraint(equalTo: view.topAnchor)

// Constrictor
label.constrictor.top(to: view)

Another anchor and an offset? Do it as follows:

// NSLayoutAnchor
label.topAnchor.constraint(equalTo: view.bottomAnchor, constant: 10)

// Constrictor
label.constrictor.top(to: view, .bottom, with: 10)

Relation, priority and inactive? Not a problem!

// NSLayoutAnchor
let constraint = label.topAnchor.constraint(greaterThanOrEqualTo: view.topAnchor)
constraint.priority = .defaultHigh

// Constrictor
label.constrictor.top(as: .greaterOrEqual, to: view, prioritizeAs: .high, is: .disabled)

Edge

How you constrain edges with NSLayoutAnchor:

label.topAnchor.constraint(equalTo: view.topAnchor)
label.bottomAnchor.constraint(equalTo: label.bottomAnchor.constraint)
label.leadingAnchor.constraint(equalTo: view.leadingAnchor)
label.trailingAnchor.constraint(equalTo: view.trailingAnchor)

How you can easily do it with Constrictor:

label.constrictor.edge(to: view)

Want to change the spacing in leading and trailing?

label.constrictor.edge(to: view, with: .horizontal(15))

What if you want to constrain every edge except bottom?

label.constrictor.edge(to: view, .top, .leading, .trailing, with: .horizontal(15))

Center

Centering with NSLayoutAnchor:

label.centerXAnchor.constraint(equalTo: view.centerXAnchor)
label.centerYAnchor.constraint(equalTo: view.centerXAnchor)

With Constrictor:

label.constrictor.center(in: label)

Different offsets?

label.constrictor.center(in: label, with: .centerX(-16) & .centerY(32))

Size

Defining size with NSLayoutAnchor:

label.widthAnchor.constraint(equalToConstant: 10)
label.heightAnchor.constraint(equalTo: label.widthAnchor)

Constrictor does it better:

label.constrictor.size(to: 10)

To another view with multiplier? Just like this:

label.constrictor.size(view, multiplyBy: 0.75)

Animate

Everybody loves animations, so does Constrictor:

// Only have one constraint for an anchor?
label.constrictor.updateFirst(.top) { $0?.enable() }

// Have two constraints for an anchor but for different elements? Provide more details
label.constrictor.update(.bottom, to: imageView) { $0?.constant = 16 }

// Call UIView.animate(...) { view.layoutIfNeeded() } to animate changes

Chain

One of the key features of Constrictor is how you can easily chain with it. As an example:

label.constrictor
.size(view, multiplyBy: 0.75)
.center(view)
.bottom(as: .greaterOrEqual, to: imageView, .top)

Installation 📦

CocoaPods

Constrictor’s available through CocoaPods. To do so, add the following line to your PodFile:

pod 'Constrictor'

And then run the following command in terminal:

pod install

Carthage

Add this to your Cartfile:

github "pedrommcarrasco/Constrictor"

And then run the following command in terminal:

carthage update

Sample Project 📲

There’s a sample project in this repository called Example, if you want to take a look at Constrictor before using it in your projects, feel free to take a look at it and try to apply some constraints with it.