用宏来创建约束规则
用约束规则来排布控件的位置是相当可靠的。不过,就其本身来说,他们非常繁琐而且特别冗长。开发者要一次又一次的编写很难懂的方法调用语句。
约束规则调用起来也特别麻烦。一个简单的拼写错误就会耗费很多时间,而且许多应用程序所使用的约束规则都是一样的。如果可以预先定义一些宏,那么就能把排布视图所用的代码写的更易懂且更加可靠。假如要把某视图与另外一个视图居中对齐,那么直接使用名为CENTER_VIEW的宏就好,而不用每次都编写约束规则并调试他们。
宏
这些宏以一种相当简单的方式来产生约束规则。请注意,他们并不把创建出来的NSLayoutConstraint对象返回给调用者,而是将这些约束规则添加到适当的视图里。如果需要获取这些新创建好的约束规则,以便稍后移除,那么可以自己添加一些能返回相关约束规则的宏。
#define PREPCONSTRAINTS(VIEW) [VIEW setTranslatesAutoresizingMaskIntoConstraints:NO]
#define CONSTRAIN(PARENT,VIEW,FORMAT) [PARENT addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:(FORMAT) options:0 metrics:nil views:NSDictionaryOfVariableBindings(VIEW)]]
#define CONSTRAIN_VIEWS(PARENT,FORMAT,BINDINGS) [PARENT addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:(FORMAT) options:0 metrics:nil views:BINDINGS]]
#define STRETCH_VIEW_H(PARENT,VIEW) CONSTRAIN(PARENT,VIEW,@"H:|["#VIEW"(>=0)]|")
#define STRETCH_VIEW_V(PARENT,VIEW) CONSTRAIN(PARENT,VIEW,@"V:|["#VIEW"(>=0)]|")
#define STRETCH_VIEW(PARENT,VIEW) {STRETCH_VIEW_H(PARENT,VIEW);STRETCH_VIEW_V(PARENT,VIEW)}
#define CENTER_VIEW_H(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]]
#define CENTER_VIEW_V(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]]
#define CENTER_VIEW(PARENT,VIEW) {ENTER_VIEW_H(PARENT,VIEW);ENTER_VIEW_V(PARENT,VIEW)}
#define ALIGN_VIEW_LEFT(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]
#define ALIGN_VIEW_RIGHT(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeRight multiplier:1 constant:0]]
#define ALIGN_VIEW_TOP(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeTop multiplier:1 constant:0]]
#define ALIGN_VIEW_BOTTOM(PARENT,VIEW) [PARENT addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:PARENT attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]
#define CONSTRAIN_WIDTH(VIEW,WIDTH) [VIEW addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:WIDTH]]
#define CONSTRAIN_HEIGHT(VIEW,HEIGHT) [VIEW addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:HEIGHT]]
#define CONSTRAIN_SIZE(VIEW,HEIGHT,WIDTH) {CONSTRAIN_WIDTH(VIEW,WIDTH);CONSTRAIN_HEIGHT(VIEW,HEIGHT)}
#define CONTRAIN_ASPECT(VIEW,ASPECT) [VIEW addConstraint:[NSLayoutConstraint constraintWithItem:VIEW attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:VIEW attribute:NSLayoutAttributeHeight multiplier:(ASPECT) constant:0]]
#define CONTRAIN_ORDER_H(PARENT,VIEW1,VIEW2) [PARENT addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:(@"H:["#VIEW1"]->=0-["#VIEW2"]") options:0 metrics:nil views:NSDictionaryOfVariableBindings(VIEW1,VIEW2)]]
#define CONTRAIN_ORDER_V(PARENT,VIEW1,VIEW2) [PARENT addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:(@"V:["#VIEW1"]->=0-["#VIEW2"]") options:0 metrics:nil views:NSDictionaryOfVariableBindings(VIEW1,VIEW2)]]