Masonry简单用法 (一)

作者: 南方_H | 来源:发表于2016-02-18 17:43 被阅读424次

    Masonry是一个布局框架。

    下载地址:https://github.com/Masonry/Masonry

    安装到项目的时候,最好是用CocoaPods集成,CocoaPods用法前面已经讲过了,不会的童鞋可以翻到前面去看下。

    我们先来看一个简单的例子,Masonry官方提供的,来简单看一下Masonry的方便之处:

    UIView *superview = self;
    
    UIView *view1 = [[UIView alloc] init];
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    view1.backgroundColor = [UIColor greenColor];
    [superview addSubview:view1];
    
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
    
    [superview addConstraints:@[
    
        //view1 constraints
        [NSLayoutConstraint constraintWithItem:view1
                                     attribute:NSLayoutAttributeTop
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:superview
                                     attribute:NSLayoutAttributeTop
                                    multiplier:1.0
                                      constant:padding.top],
    
        [NSLayoutConstraint constraintWithItem:view1
                                     attribute:NSLayoutAttributeLeft
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:superview
                                     attribute:NSLayoutAttributeLeft
                                    multiplier:1.0
                                      constant:padding.left],
    
        [NSLayoutConstraint constraintWithItem:view1
                                     attribute:NSLayoutAttributeBottom
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:superview
                                     attribute:NSLayoutAttributeBottom
                                    multiplier:1.0
                                      constant:-padding.bottom],
    
        [NSLayoutConstraint constraintWithItem:view1
                                     attribute:NSLayoutAttributeRight
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:superview
                                     attribute:NSLayoutAttributeRight
                                    multiplier:1
                                      constant:-padding.right],
    
     ]];
    

    用Masonry添加同样约束代码如下:

     UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
        
        [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
            make.left.equalTo(superview.mas_left).with.offset(padding.left);
            make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
            make.right.equalTo(superview.mas_right).with.offset(-padding.right);
        }];
    

    或者一句代码

    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superview).with.insets(padding);
    }];
    

    那我们就一点点的去分析,为什么Masonry可以这么简便的做到这些。
    我们先看mas_makeConstraints这个方法:

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

    block内部创建constraintMaker,接着插入block中当参数,并执行install方法,确保约束能添加到相应的视图上。

        make.edges.equalTo(superview).with.insets(padding);
    

    make:MASConstraintMaker
    edges:需要添加约束视图的边缘,相对于equalTo(superview)来说。
    with:点进去可以看到返回的是self,这样是masonry的语法更清晰。
    insets:需要添加约束的条件。参数为padding,也就是上下左右距离10。
    这句代码用汉语翻译过来的意思就是:
    view1的约束添加 边缘相对于(他的父视图superview)来说执行添加(左右上下10的距离);

    MASConstraintMaker 属性

    Masonry NSLayout 翻译
    left NSLayoutAttributeLeft 左侧
    top NSLayoutAttributeTop 上侧
    right NSLayoutAttributeRight 右侧
    bottom NSLayoutAttributeBottom 下侧
    leading NSLayoutAttributeLeading 首部
    trailing NSLayoutAttributeTrailing 尾部
    width NSLayoutAttributeWidth 宽度
    height NSLayoutAttributeHeight 高度
    centerX NSLayoutAttributeCenterX 横向中心点
    centerY NSLayoutAttributeCenterY 竖向中心点
    baseline NSLayoutAttributeBaseline 文本基线

    我们还看上面的例子来继续分析属性与参数

     UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
        
        [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
            make.left.equalTo(superview.mas_left).with.offset(padding.left);
            make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
            make.right.equalTo(superview.mas_right).with.offset(-padding.right);
        }];
    

    make.top.equalTo(superview.mas_top).with.offset(padding.top);
    equalTo():约束针对于(),参数可以是NSNumber,可以是UIEdgeInsets,可以是父视图,甚至可以填写superview.mas_left。

    相关文章

      网友评论

        本文标题:Masonry简单用法 (一)

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