CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✓ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Nov 2015 |
| SPMSupports SPM | ✗ |
Maintained by Alex Pretzlav.
Strutter is a Swift µFramework for defining AutoLayout constraints with a few custom infix operators. It uses iOS 9 / OS X 10.11’s NSLayoutAnchor system for defining these constraints.
For backwards compatibility, there is a subspec called OALayoutAnchor and an alternate framework called Strutter iOS OALayout which uses OALayoutAnchor instead of NSLayoutAnchor. This version works on iOS 8.
Strutter uses the custom operators |=|, |>=|, and |<=| to define equal, greater than equal, and less than equal constraints between two NSLayoutAnchors. The Apple documentation explains the basics of how NSLayoutAnchor works.
Strutter automatically activates constraints and disables the translatesAutoresizingMaskIntoConstraints attribute of the left-hand view. It doesn’t automatically disable both views so positioning subviews of view controllers and navigation bars works as expected.
Since Strutter is just syntactic sugar on top of NSLayoutAnchor’s methods, it retains the type safety enforced by NSLayoutAnchor which prevents creating invalid constraints, such as between dimension and location metrics.
Strutter supports Cocoapods and Carthage.
This verbose blob aligns the trailing edge of one view with the leading edge of another on iOS 8:
NSLayoutConstraint(item: view1,
attribute: .Trailing,
relatedBy: .Equal,
toItem: view2,
attribute: .Leading,
multiplier: 1,
constant: 0).active = trueUsing layout anchors shortens this to the following:
view1.trailingAnchor.constraintEqualToAnchor(view2.leadingAnchor).active = trueStrutter shortens this futher to
view1.trailingAnchor |=| view2.leadingAnchorThe value of a Strutter expression is the created constraint, so it can be deactivated after creation if desired:
let constraint = view1.trailingAnchor |=| view2.leadingAnchor
constraint.active = falseStrutter allows specifying a constant and/or multiplier via tuples:
subview.topAnchor |=| (superview.topAnchor, 10)is equivalent to
subview.topAnchor.constraintEqualToAnchor(
superview.topAnchor, constant: 10).active = true
// or
NSLayoutConstraint(item: subview,
attribute: .Top,
relatedBy: .Equal,
toItem: superview,
attribute: .Top,
multiplier: 1,
constant: 10).active = trueand
subv.heightAnchor |=| (v.heightAnchor, multiplier: 2, constant: -20)is equivalent to
subv.heightAnchor.constraintEqualToAnchor(
v.heightAnchor, multiplier: 2, constant: -20)To define less-than and greater-than attributes:
subv.heightAnchor |<=| v.heightAnchorIs the same as
subv.heightAnchor.constraintLessThanOrEqualToAnchor(v.heightAnchor)Lastly, Strutter allows constraining to constant values for dimension axes:
subv.heightAnchor |=| 200is equivalent to
subv.heightAnchor.constraintEqualToConstant(200)Take a look at the playground, which demonstrates basic Strutter syntax. To use it, open the Strutter.xcworkspace project, build the Strutter iOS scheme with a target of an iPhone 5s simulator or newer, and select Example.playground in the project navigator.
Strutter is released under the MIT License. Contributions appreciated.