Masonry浅析
来源:互联网 发布:淘宝win10激活码来源 编辑:程序博客网 时间:2024/05/21 17:47
首先我们可以先写下最常用的代码
[btn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.equalTo(@10); make.right.bottom.equalTo(@10);}
点进这个方法mas_makeConstraints,我们会看到下面的这样的代码,先把autoresizing先关闭,autoresizing其实就是根据父控件的大小去拉伸的,然后去创建了这个MASConstraintMaker类的对象,其实这个类的对象也就是make就是约束制造者。
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block { self.translatesAutoresizingMaskIntoConstraints = NO; MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; block(constraintMaker); return [constraintMaker install];}我们再去看上面的initWithView的代码,其实就是去绑定了一个view,然后去创建了一个约束数组。
- (id)initWithView:(MAS_VIEW *)view { self = [super init]; if (!self) return nil; self.view = view; self.constraints = NSMutableArray.new; return self;}然后那个其实就是把constraintMaker传递给外面,然后调用外面写好的block。
block(constraintMaker)
再就是去安装约束点击进入install的方法,其实就是先把view之前的约束先清空了,然后再把约束数组中有的约束给安装上去
[constraintMaker install]也就是下面的代码
- (NSArray *)install { if (self.removeExisting) { NSArray *installedConstraints = [MASViewConstraint installedConstraintsForView:self.view]; for (MASConstraint *constraint in installedConstraints) { [constraint uninstall]; } } NSArray *constraints = self.constraints.copy; for (MASConstraint *constraint in constraints) { constraint.updateExisting = self.updateExisting; [constraint install]; } [self.constraints removeAllObjects]; return constraints;}之前我们再去看看单个约束安装的代码也就是下面这个install的方法
[constraint install];
我们点进去看看,注意的是我们这里点击的是MASViewConstraint,因为我们添加的是left,top等约束,就不复制所有的代码了,复制一部分,其实就是在做创建约束和一些其他事情的处理。
MASLayoutConstraint *layoutConstraint = [MASLayoutConstraint constraintWithItem:firstLayoutItem attribute:firstLayoutAttribute relatedBy:self.layoutRelation toItem:secondLayoutItem attribute:secondLayoutAttribute multiplier:self.layoutMultiplier constant:self.layoutConstant]; layoutConstraint.priority = self.layoutPriority; layoutConstraint.mas_key = self.mas_key; if (self.secondViewAttribute.view) { MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view]; NSAssert(closestCommonSuperview, @"couldn't find a common superview for %@ and %@", self.firstViewAttribute.view, self.secondViewAttribute.view); self.installedView = closestCommonSuperview; } else if (self.firstViewAttribute.isSizeAttribute) { self.installedView = self.firstViewAttribute.view; } else { self.installedView = self.firstViewAttribute.view.superview; }然后我们再去点击我们设置的left,top等代码进去看其实就是去添加个约束属性
- (MASConstraint *)left { return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft];}然后再这样一层一层的点击进去,会发现其实就是去给self.view去绑定属性,之后去创建MASViewConstraint这个类的对象,然后设置代理之后添加进约束数组中,截取一部分代码
MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute]; MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute]; if (!constraint) { newConstraint.delegate = self; [self.constraints addObject:newConstraint]; }
阅读全文