美文网首页
Masonry布局

Masonry布局

作者: LX2014 | 来源:发表于2018-02-19 17:42 被阅读9次

    参考:https://www.jianshu.com/p/e3162f3c61fa

    1,masonry进行线性布局

    //首先添加5个视图
     NSMutableArray *array = [NSMutableArray new];
        for (int i = 0; i < 5; i ++) {
            UIView *view = [UIView new];
            view.backgroundColor = [UIColor greenColor];
            [self addSubview:view];
            [array addObject:view]; //保存添加的控件
        }
    
    //水平方向控件间隔固定等间隔
    [array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15 leadSpacing:10 tailSpacing:10];
                [array makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(50);
                    make.height.equalTo(70);
                }];
    
    //水平方向宽度固定等间隔
    [array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:70 leadSpacing:10 tailSpacing:10];
                [array makeConstraints:^(MASConstraintMaker *make) { //数组额你不必须都是view 
                    make.top.equalTo(50);
                    make.height.equalTo(70);
                }];
    

    2,masonry不会造成循环引用的原因

    传入的block直接就同步执行了,执行完block引入的变量引用计数又会释放。见如下代码:

    - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
        self.translatesAutoresizingMaskIntoConstraints = NO;
        MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
        block(constraintMaker);
        return [constraintMaker install];
    }
    

    相关文章

      网友评论

          本文标题:Masonry布局

          本文链接:https://www.haomeiwen.com/subject/rbaitftx.html