Skip to content

pedrommcarrasco/Constrictor

Repository files navigation

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

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

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

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

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)

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

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.

Support Constrictor ❀️

Hello there πŸ‘‹

I’m Pedro, a Portuguese iOS Engineer since February 2017. I’m an avid OSS enthusiast and contributor - help by sharing, learn by what’s shared.

I've built and open-sourced multiple frameworks and applications, including Brooklyn and CocoaHub.

I'm also a conference and meetup organizer, being part of SwiftAveiro and CocoaHeads Porto.

If you enjoy my work and would like to help me continue it, please consider:

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.