Constrictor 6.0.1

Constrictor 6.0.1

Maintained by Pedro Carrasco.

Constrict (/ˈkΙ™nˈstrΙͺkt/), verb

"... to make something become tighter and narrower:"

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


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 ⌨️


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

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


Constrictor allows you to fully replace NSLayoutAnchor. For example:

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

// Constrictor view)

Another anchor and an offset? Do it as follows:

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

// Constrictor view, .bottom, with: 10)

Relation, priority and inactive? Not a problem!

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

// Constrictor .greaterOrEqual, to: view, prioritizeAs: .high, is: .disabled)


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))


Centering with NSLayoutAnchor:

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

With Constrictor: label)

Different offsets? label, with: .centerX(-16) & .centerY(32))


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)


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

Installation πŸ“¦


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


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.

Contributing πŸ™Œ

Feel free to contribute to this project by reporting bugs or open pull requests. Also, feel free to ask me anything on Twitter

License β›”

Constrictor's available under the MIT license. See the LICENSE file for more information.