TATLayout 旨在在很大程度上减少在 iOS 中编写布局约束时所用的代码行数。它提供了一组高级 API,使得布局代码更容易阅读、维护和动态修改。
NSLayoutConstraint
中的类别,提供了一个以线性方程式格式字符串创建约束的工厂方法。NSLayoutConstraint
中的类别,提供了激活和停用方法,这些可以在 iOS 6+ 上使用,但在可用时使用 iOS 8。iOS 6.0+
TATLayout 通过 CocoaPods 和作为静态库提供。请查看 Wiki 获取详细的安装步骤。
查看 文档 以全面了解 TATLayout 中所有可用的 API。
#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 when available)
c.tat_active = YES;
c.tat_active = NO;
// Multiple (uses iOS 8 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];
通过打开 TATLayout.xcworkspace 并运行 TATLayoutExample 方案来尝试示例应用。如果您还没有克隆项目,可以使用 CocoaPods 的 try
命令
$ pod try TATLayout
TATLayout 由单元测试驱动。为了运行测试,您必须通过 CocoaPods 安装测试依赖项
$ pod install
测试依赖项安装完毕后,在 Xcode 中打开 TATLayout.xcworkspace,确保您已选择 TATLayoutExample 方案,然后按下 command+U。
TATLayout 使用 Kiwi 进行单元测试。
该项目采用 MIT 许可证。