MASConstraintMaker是Masonry框架運轉的核心對象之一,我們知道,Masonry是基于AutoLayout技術的封裝,所以我們來探尋一下MASConstraintMaker是如何發揮作用的。
先看一段典型的用法介紹:
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
make.left.equalTo(superview.mas_left).with.offset(padding.left);
make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];
第一部分 MASConstraintMaker與AutoLayout關系梳理
一、我們常用的 mas_makeConstraints方法的實現
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
block(constraintMaker);
return [constraintMaker install];
}
方法內部創建 MASConstraintMaker對象,然后傳遞到block中,執行完block,調用 [constraintMaker install]來確保約束被添加到視圖中去。
二、上面MASConstraintMaker對象的[constraintMaker install]方法
for (MASConstraint *constraint in constraints) {
constraint.updateExisting = self.updateExisting;
[constraint install];
}
可以看到,MASConstraintMaker包含很多的MASConstraint對象,然后逐一調用MASConstraint對象的install方法。
MASViewConstraint是MASConstraint的子類,創建NSLayoutConstraint添加到視圖中去,看一下install方法:
...
MASLayoutConstraint *layoutConstraint
= [MASLayoutConstraint constraintWithItem:firstLayoutItem
attribute:firstLayoutAttribute
relatedBy:self.layoutRelation
toItem:secondLayoutItem
attribute:secondLayoutAttribute
multiplier:self.layoutMultiplier
constant:self.layoutConstant];
...
...
...
[self.installedView addConstraint:layoutConstraint];
第二部分 查看MASConstraintMaker頭文件
一、頭文件第一部分
@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *leading;
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;
#if TARGET_OS_IPHONE
@property (nonatomic, strong, readonly) MASConstraint *leftMargin;
@property (nonatomic, strong, readonly) MASConstraint *rightMargin;
@property (nonatomic, strong, readonly) MASConstraint *topMargin;
@property (nonatomic, strong, readonly) MASConstraint *bottomMargin;
@property (nonatomic, strong, readonly) MASConstraint *leadingMargin;
@property (nonatomic, strong, readonly) MASConstraint *trailingMargin;
@property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins;
@property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins;
#endif
上面這些是比較常規的一些屬性,在一開始的例子中
make.top.equalTo(superview.mas_top).with.offset(padding.top);
1、make.top??-->??返回一個MASConstraint對象</br>
2、top.equalTo??-->??返回一個block對象,equalTo對應的AutoLayout關系就是NSLayoutRelationEqual,block的接收參數是id類型,block返回值是MASConstraint類型</br>
3、equalTo(superview.mas_top)??-->??調用block,返回MASConstraint類型對象</br>
4、with可以忽略,offset類似,這里不詳解MASConstraint對象
二、頭文件第二部分
@property (nonatomic, strong, readonly) MASConstraint *(^attributes)(MASAttribute attrs);
@property (nonatomic, strong, readonly) MASConstraint *edges;
@property (nonatomic, strong, readonly) MASConstraint *size;
@property (nonatomic, strong, readonly) MASConstraint *center;
1、attributes??-->??返回一個block對象,block的接收參數是MASAttribute類型,返回MASCompositeConstraint對象</br>
2、edges??-->??返回一個MASConstraint對象,同時包含了上下左右的布局信息</br>
3、size??-->??返回一個MASConstraint對象,同時包含了寬高的布局信息</br>
4、center??-->??返回一個MASConstraint對象,同時包含了centerX和centerY信息