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];    }







阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红斑蛇有毒吗 蛇毒血凝酶一支多少钱 白眉蛇毒血凝酶说明书 大王蛇有毒吗 蛇毒眼霜 蛇毒啤酒多钱一瓶 蛇毒啤酒70度 蛇毒眼膜敷多久 蛇毒价格表 蛇毒的功效与作用 蛇毒作用 蛇毒图片 蛇毒的作用 蛇毒价格多少一克 蛇毒价格 马为什么不怕蛇毒 蛇汤 蛇血 灰蛇 奶蛇价格 蛇什么膏 蛇油 蛇油的功效 泰国蛇油 纯蛇油擦脸上有什么功效 隆力奇蛇油护手霜50g 蛇油膏的功效与作用 泰国蛇油丸说明书 蛇油膏的作用 蛇油膏批发 大宝蛇油sod蜜 隆力奇蛇油sod蜜 泰国蛇油作用 蛇油祛痘 蛇油防裂膏 蛇油冻疮膏 蛇油丸 蛇油疤痕膏 蛇油护手霜 蛇油能去疤吗 蛇油护手霜可以擦脸吗