一标捺、封裝結(jié)構(gòu)
MASConstraint 約束
MASCompositeConstraint 一組約束//
MASViewConstraint 一個(gè)約束//囊括整個(gè)約束計(jì)算公式的所有數(shù)據(jù)(firstViewAttribute、secondViewAttribute揉抵、常量值亡容、關(guān)系、優(yōu)先級(jí)冤今、比例值闺兢。用于鏈?zhǔn)讲僮鞯摹癲elegate”)。
MASConstraintMaker 創(chuàng)建約束的工廠
MASViewAttribute 封裝 View+約束, 等式兩邊的計(jì)算因子(結(jié)構(gòu)相當(dāng)于元組類型)
View+MASAdditions 入口( 創(chuàng)建計(jì)算MASViewAttribute的工廠 )
ViewController+MASAdditions 入口( 創(chuàng)建MASViewAttribute的工廠 )
NSArray+MASAdditions 對(duì)數(shù)組中的每一個(gè)view做相同的約束操作屋谭。 批量操作脚囊。還提供了一套統(tǒng)一布局的方法。(固定大小桐磁,固定間距)
MASUtilities 用來(lái)將任何類型(數(shù)值類型悔耘,對(duì)象類型), 轉(zhuǎn)化為對(duì)象類型
二我擂、鏈?zhǔn)?/p>
1衬以、
make.height.inset(10);//無(wú)效。因?yàn)閘ayoutAttribute是NSLayoutAttributeHeight
make.height.mas_equalTo(10);
2校摩、
make.height.mas_equalTo(10); // 如果是NSValue類型看峻。給firstView設(shè)置
make.height.mas_equalTo(self.view);// 如果是View類型, 創(chuàng)建secondViewAttribute衙吩。并且屬性NSLayoutAttribute與firstViewAttribute相同
make.height.mas_equalTo(self.view.mas_height); //如果是計(jì)算因子類型互妓。則直接賦值。
3分井,
鏈?zhǔn)饺绾螌?shí)現(xiàn)的车猬?
MASCompositeConstraint(一組約束) 代理霉猛。
MASConstraintMaker (創(chuàng)建約束的工廠) 代理尺锚。
//操作1
make.top.offset(0);
make.bottom.offset(0);
make.size.sizeOffset(CGSizeZero);
//鏈?zhǔn)?
make.top.left.right.offset(0);
//鏈?zhǔn)?
make.edges.top.left.offset(0);
//鏈?zhǔn)?
make.top.equalTo(@20).equalTo(@30);
// 不支持這種鏈?zhǔn)剑?后面的-10會(huì)覆蓋前面的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;
}////////
//不支持重復(fù)添加約束
//equalTo(v2.mas_left)第二個(gè)約束,重復(fù)添加惜浅。
//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);
// 不支持這種鏈?zhǔn)?br> 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瘫辩、設(shè)置布局值
嘗試用不支持的值設(shè)置布局常量.attempting to set layout constant with unsupported value
NSNumber,CGPoint坛悉,CGSize伐厌,UIEdgeInsets
嘗試添加不支持的屬性attempting to add unsupported attribute
UIView、NSValue裸影、MASViewAttribute
offset只是改變約束常量值
equalTo會(huì)增加約束關(guān)系(secondeViewAttribute)挣轨,如果是NSValue類型(與offset效果相似),根據(jù)約束的布局屬性NSLayoutAttribute是否匹配轩猩,設(shè)置對(duì)應(yīng)的常量值卷扮。
//相同
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 , 會(huì)造成崩潰
Constraint improperly relates anchors of incompatible types
make.top.equalTo(v2.mas_left);//崩潰
make.top.equalTo(@[v2.mas_top,v2.mas_left]);//崩潰
如果是第一個(gè)View不是大小約束均践,且第二個(gè)Item是nil晤锹,則第二個(gè)Item自動(dòng)設(shè)置為第一個(gè)View的父View,屬性關(guān)系與第一個(gè)View的屬性相同彤委。
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;