zoukankan      html  css  js  c++  java
  • 使用第三方框架 Masonry 实现自动布局的简单使用

    苹果自带的原生自动布局方案太过繁琐,在此介绍一个简单好用的第三方框架。

    1 //设置约束
    2 - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block;
    3 
    4 //如果之前已经有约束,则更新新的约束,如果没有约束,则添加约束
    5 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block;
    6 
    7 //将之前的约束全部删除,添加新的约束
    8 - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
    注:
    使用前需把自定义的控件的 translatesAutoresizingMaskIntoConstraints属性设为NO;

    //添加两个控件

    UIView *blueView = [[UIView alloc] init];

    blueView.backgroundColor = [UIColor blueColor];

    blueView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:blueView];

    UIView *redView = [[UIView alloc] init];

    redView.backgroundColor = [UIColor redColor];

    redView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:redView];

    //给蓝色View设置约束

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view.mas_left).offset(30);//和父view的左边间距为30;

        make.bottom.equalTo(self.view.mas_bottom).offset(-30);//和父view的底部间距为30;

        make.right.equalTo(redView.mas_left).offset(-30);//和红色view的间距为30;

        make.height.mas_equalTo(50);//蓝色view的高度为50

    }];

    //给红色View设置约束

    [redView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.equalTo(self.view.mas_right).offset(-30);//和父view的右边间距为30;

        make.bottom.equalTo(blueView.mas_bottom);//和蓝色view的底部对齐

        make.height.equalTo(blueView.mas_height);//和蓝色view的高度相等

        make.width.equalTo(blueView.mas_width);//和蓝色view的宽度相等

    }];

    
    
  • 相关阅读:
    lab4:VGG16 及 如何把c的for转成verliog状态机
    pl向ps传输8位数据:axi-lite
    lab3:LeNet上板
    UVALive5902:Movie collection
    UVa11069:A Graph Problem
    UVa10323:Factorial! You Must be Kidding!!!
    UVa10220:I Love Big Numbers !
    UVa623:500!
    UVa568:Just the Facts
    UVa11549:Calculator Conundrum
  • 原文地址:https://www.cnblogs.com/paideblogs/p/4987325.html
Copyright © 2011-2022 走看看