Constrict (/ΛkΙnΛstrΙͺkt/), verb
"... to make something become tighter and narrower:"
Constrict your Auto Layout code with Constrictor, your chainable sugar.
- Compatible with Auto Layout π
- Concise and chainable syntax βοΈ
- Automatically sets
translateAutoresizingMaskIntoConstraints
π - Constraints are active by default π€©
- Easily update constraints π
- Allows setting priority upon creation πββοΈ
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)
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)
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.constrictor.center(in: label)
Different offsets?
label.constrictor.center(in: 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
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
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.
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:
- https://github.com/users/pedrommcarrasco/sponsorship
- https://www.buymeacoffee.com/pedrommcarrasco
- https://www.paypal.me/pedrommcarrasco
Feel free to contribute to this project by reporting bugs or open pull requests. Also, feel free to ask me anything on Twitter
Constrictor's available under the MIT license. See the LICENSE file for more information.