一、封裝結(jié)構(gòu)
MASConstraint 約束
MASCompositeConstraint 一組約束//
MASViewConstraint 一個約束//囊括整個約束計算公式的所有數(shù)據(jù)(firstViewAttribute、secondViewAttribute、常量值、關(guān)系、優(yōu)先級、比例值。用于鏈式操作的“delegate”)。
MASConstraintMaker 創(chuàng)建約束的工廠
MASViewAttribute 封裝 View+約束, 等式兩邊的計算因子(結(jié)構(gòu)相當于元組類型)
View+MASAdditions 入口( 創(chuàng)建計算MASViewAttribute的工廠 )
ViewController+MASAdditions 入口( 創(chuàng)建MASViewAttribute的工廠 )
NSArray+MASAdditions 對數(shù)組中的每一個view做相同的約束操作。 批量操作。還提供了一套統(tǒng)一布局的方法。(固定大小,固定間距)
MASUtilities 用來將任何類型(數(shù)值類型,對象類型), 轉(zhuǎn)化為對象類型
二、鏈式
1、
make.height.inset(10);//無效。因為layoutAttribute是NSLayoutAttributeHeight
make.height.mas_equalTo(10);
2、
make.height.mas_equalTo(10); // 如果是NSValue類型。給firstView設置
make.height.mas_equalTo(self.view);// 如果是View類型, 創(chuàng)建secondViewAttribute。并且屬性NSLayoutAttribute與firstViewAttribute相同
make.height.mas_equalTo(self.view.mas_height); //如果是計算因子類型。則直接賦值。
3,
鏈式如何實現(xiàn)的?
MASCompositeConstraint(一組約束) 代理。
MASConstraintMaker (創(chuàng)建約束的工廠) 代理。
//操作1
make.top.offset(0);
make.bottom.offset(0);
make.size.sizeOffset(CGSizeZero);
//鏈式1
make.top.left.right.offset(0);
//鏈式2
make.edges.top.left.offset(0);
//鏈式3
make.top.equalTo(@20).equalTo(@30);
// 不支持這種鏈式: 后面的-10會覆蓋前面的10
make.top.left.equalTo(@10).bottom.right.equalTo(@(-10));
MASLayoutConstraint *existingConstraint = nil;
if (self.updateExisting) {
existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint];
}
if (existingConstraint) {
// just update the constant
existingConstraint.constant = layoutConstraint.constant;
self.layoutConstraint = existingConstraint;
}////////
//不支持重復添加約束
//equalTo(v2.mas_left)第二個約束,重復添加。
//NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation");
make.top.left.equalTo(v2.mas_top).equalTo(v2.mas_left).equalTo(@0);
// 不支持這種鏈式
make.center.size.edges
masConstraint
#pragma mark - attribute chaining
- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
NSAssert(!self.hasLayoutRelation, @"Attributes should be chained before defining the constraint relation");
return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
}
masCompositeConstraint
#pragma mark - attribute chaining
- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
[self constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
return self;
}
- (MASConstraint *)constraint:(MASConstraint __unused *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
id<MASConstraintDelegate> strongDelegate = self.delegate;
MASConstraint *newConstraint = [strongDelegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
newConstraint.delegate = self;
[self.childConstraints addObject:newConstraint];
return newConstraint;
}
- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint {
NSUInteger index = [self.childConstraints indexOfObject:constraint];
NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint);
[self.childConstraints replaceObjectAtIndex:index withObject:replacementConstraint];
}
maker
- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute];
}
- (MASConstraint *)addConstraintWithAttributes:(MASAttribute)attrs {
/////
NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count];
for (MASViewAttribute *a in attributes) {
[children addObject:[[MASViewConstraint alloc] initWithFirstViewAttribute:a]];
}
MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children];
constraint.delegate = self;
[self.constraints addObject:constraint];
return constraint;
}
- (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute];
MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute];
if ([constraint isKindOfClass:MASViewConstraint.class]) {
//replace with composite constraint
NSArray *children = @[constraint, newConstraint];
MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
compositeConstraint.delegate = self;
[self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint];
return compositeConstraint;
}
if (!constraint) {
newConstraint.delegate = self;
[self.constraints addObject:newConstraint];
}
return newConstraint;
}
- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint {
NSUInteger index = [self.constraints indexOfObject:constraint];
NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint);
[self.constraints replaceObjectAtIndex:index withObject:replacementConstraint];
}
4、設置布局值
嘗試用不支持的值設置布局常量.attempting to set layout constant with unsupported value
NSNumber,CGPoint,CGSize,UIEdgeInsets
嘗試添加不支持的屬性attempting to add unsupported attribute
UIView、NSValue、MASViewAttribute
offset只是改變約束常量值
equalTo會增加約束關(guān)系(secondeViewAttribute),如果是NSValue類型(與offset效果相似),根據(jù)約束的布局屬性NSLayoutAttribute是否匹配,設置對應的常量值。
//相同
make.left.equalTo(@20);
make.left.offset(20);
//相同
make.left.equalTo(v2.mas_right).offset(20);
make.left.equalTo(v2.mas_right).equalTo(@20);
make.top.inset(0).insets(UIEdgeInsetsZero).offset(0).sizeOffset(CGSizeZero).centerOffset(CGPointZero).valueOffset([NSValue valueWithCGPoint:CGPointZero]).valueOffset( [NSValue value:&CGPointZero withObjCType:@encode(typeof((CGPointZero)))]);
//NSLayoutAttributeTop,NSLayoutAttributeLeft , 會造成崩潰
Constraint improperly relates anchors of incompatible types
make.top.equalTo(v2.mas_left);//崩潰
make.top.equalTo(@[v2.mas_top,v2.mas_left]);//崩潰
如果是第一個View不是大小約束,且第二個Item是nil,則第二個Item自動設置為第一個View的父View,屬性關(guān)系與第一個View的屬性相同。
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;