Autoclave is your easygoing Autolayout friend. It condenses your constraint definitions into simple chained methods.
You can write this:
AC(button).make(.CenterX, .CenterY).sameAs(view).addTo(view)
Instead of this:
view.addConstraint(NSLayoutConstraint(
item: button,
attribute: .CenterX,
relatedBy: .Equal,
toItem: view,
attribute: .CenterX,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(
item: button,
attribute: .CenterY,
relatedBy: .Equal,
toItem: view,
attribute: .CenterY,
multiplier: 1,
constant: 0))
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate Autoclave into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'Autoclave'
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Autoclave into your Xcode project using Carthage, specify it in your Cartfile
:
github "seasaltio/Autoclave"
Run carthage
to build the framework and drag the built Autoclave.framework
into your Xcode project.
Constrain CenterX
of button
as equal to view
. Add the constraint to view
.
AC(button).make(.CenterX).sameAs(view).addTo(view)
Note: addTo()
returns generated constraints if you need to reference them. E.g:
let buttonConstraints = AC(button)
.make(.CenterX)
.sameAs(view).addTo(view)
Constrain the Width
of each item (button
, image
, label
) to be the same as view
. Add the constraints to view
.
AC(button, image, label)
.make(.Width)
.sameAs(view)
.addTo(view)
Constrains the button's CenterX
, CenterY
, Width
, Height
to the respective attributes of view
. Add the constraints to view
.
AC(button).make(.CenterX, .CenterY, .Width, .Height)
.sameAs(view)
.addTo(view)
Bonus! Lots of attributes on lots of views.
AC(button, image, label)
.make(.CenterX, .CenterY, .Width, .Height)
.sameAs(view)
.addTo(view)
Constrain the Width
of each item (button
, image
, label
) to be the same as view
Height
. Add the constraints to view
.
AC(button, image, label)
.make(.Width)
.sameAs(view, attr: .Height)
.addTo(view)
AC(button).make(.Height)
.lessThan(view)
.addTo(view)
AC(button).make(.Height)
.greaterThan(view)
.addTo(view)
AC(image)
.make(.Width, .Height)
.sameAs(view)
.multiplier(0.8)
.addTo(view)
AC(image)
.make(.Width, .Height)
.sameAs(view)
.constant(-30.0)
.addTo(view)
The priority method will set a specific priority for all generated constraints.
AC(image)
.make(.Width, .Height)
.sameAs(view)
.priority(UILayoutPriorityRequired)
.addTo(view)
let constraints = AC(image)
.make(.Width, .Height)
.sameAs(view)
.constraints()
With Autoclave, you can chain multiple calls to format()
which will produce multiple constraint sets for the same view dictionary. addTo()
will add the produced constraints to a view.
let views = [
"title": titleLabel,
"body": bodyLabel,
]
AC.visual(views)
.format("V:|-[title]-[body]-|")
.format("|[title]|")
.format("|[body]|")
.addTo(view)
Note: addTo()
returns generated constraints if you need to reference them. E.g:
let constraints = AC.visual(views)
.format("V:|-[title]-[body]-|")
.addTo(view)
let metrics = [
"imageWidth": 100,
"captionWidth": 300,
"space": 5,
]
AC.visual(views)
.metrics(metrics)
.format("|[image(imageWidth)]-space-[caption(captionWidth)]|")
.addTo(view)
If you need to supply format options to your constraints, use options()
to supply and options mask.
AC.visual(views)
.format("|[image][caption]|")
.options(.AlignAllCenterX)
.addTo(view)
let constraints = AC.visual(views)
.format("V:|-[title]-[body]-|")
.format("|[title]|")
.format("|[body]|")
.constraints()