TATLayout aims to reduce considerably the amount of lines of code used when coding layout constraints in iOS. It provides a high level API to layout constraints that makes your layout code easier to read, mantain and modify dynamically.
- Category in
NSLayoutConstraint
providing a factory method for creating constraints using a linear equation format string. - Category in
NSLayoutConstraint
providing methods for activation and deactivation that can be used in iOS 6+ but uses iOS 8 when available, using feature detection. - A couple of helper methods useful for working with layouts.
iOS 6.0+
TATLayout is available through CocoaPods and as a static library. Check out the Wiki for detailed installation steps.
Check out the documentation for a comprehensive look at all of the APIs available in TATLayout.
#import <TATLayout/TATLayout.h>
UIView *view1 = [UIView new];
UIView *view2 = [UIView new];
UILabel *label = [UILabel new];
NSDictionary *metrics = @{@"multiplier": @0.5, @"constant": @50, @"priority": @751};
NSDictionary *views = NSDictionaryOfVariableBindings(view1, view2, label);
TATDisableAutoresizingConstraintsInViews(view1, view2, label);
// Creating constraints with equation format strings:
NSLayoutConstraint *c;
// view1 width is equal to 100 points
c = [NSLayoutConstraint tat_constraintWithEquationFormat:@"view1.width == 100" metrics:nil views:views];
// view1 height is equal to 2 times its width
c = [NSLayoutConstraint tat_constraintWithEquationFormat:@"view1.height == view1.width * 2" metrics:nil views:views];
// view1 center x is equal to its superview's center x
c = [NSLayoutConstraint tat_constraintWithEquationFormat:@"view1.centerX == superview.centerX" metrics:nil views:views];
// view1 top is equal to its superview's top plus 50 points
c = [NSLayoutConstraint tat_constraintWithEquationFormat:@"view1.top == superview.top + constant" metrics:metrics views:views];
// view1 bottom is less than or equal to its superview's bottom with a priority of 251
c = [NSLayoutConstraint tat_constraintWithEquationFormat:@"view1.bottom <= superview.bottom @251" metrics:nil views:views];
// view1 bottom is greater than or equal to view2's bottom with a priority of 751
c = [NSLayoutConstraint tat_constraintWithEquationFormat:@"view1.bottom >= view2.bottom @priority" metrics:metrics views:views];
// view2 top is greater than or equal to three quarters of view1's top plus 50 points with a priority of 500
c = [NSLayoutConstraint tat_constraintWithEquationFormat:@"view2.top >= view1.top * 0.75 + 50 @500" metrics:nil views:views];
// Multiple constraints can be created at once:
NSArray *constraints = [tat_constraintsWithEquationFormats:@[@"view2.leading == view1.trailing" // view2 leading is equal to view1's trailing
@"view2.trailing == superview.trailing" // view2 trailing is equal to its superview's trailing
@"view2.height == view1.height * multiplier" // view2 height is equal to half the view1's height
@"label.baseline == view1.centerY" // label baseline is equal to view1's centerY
@"label.centerX == view1.centerX"] // label is x centered with view1
metrics:metrics views:views];
// Activating and deactivating constraints:
// Single (uses iOS 8 built-in when available)
c.tat_active = YES;
c.tat_active = NO;
// Multiple (uses iOS 8 built-in when available)
[NSLayoutConstraint tat_activateConstraints:constraints];
[NSLayoutConstraint tat_deactivateConstraints:constraints];
// Creating and activating constraints in the same operation:
[NSLayoutConstraint tat_activateConstraintWithEquationFormat:@"label.leading == view1.trailing" metrics:metrics views:views];
[NSLayoutConstraint tat_activateConstraintsWithEquationFormats:@[@"label.leading == view1.trailing"
@"label.trailing == view2.leading",
@"label.baseline == view1.centerY"]
metrics:metrics views:views];
[NSLayoutConstraint tat_activateConstraintsWithVisualFormat:@"H:|[view1][label][view2]|" options:NSLayoutFormatAlignAllTop metrics:metrics views:views];
Try out the example app by opening TATLayout.xcworkspace and running TATLayoutExample scheme. If you haven't cloned the project you can use CocoaPods try
command:
$ pod try TATLayout
TATLayout is driven by unit testing. In order to run the tests, you must install the testing dependencies via CocoaPods:
$ pod install
Once testing dependencies are installed, open TATLayout.xcworkspace in Xcode, make sure you have selected TATLayoutExample scheme and hit command+U.
The documentation is available online through CocoaDocs.
TATLayout uses Kiwi for unit testing.
This project is under the MIT license.