Masonry使用

// 解决 mas_

//define this constant if you want to use Masonry without the 'mas_' prefix

#define MAS_SHORTHAND


// 解决 对数据的自动装箱

//define this constant if you want to enable auto-boxing for default syntax

#define MAS_SHORTHAND_GLOBALS

#import "Masonry.h"



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

    redView.backgroundColor=[UIColor redColor];

    [self.view addSubview: redView];

    [redView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.view.mas_top).offset(20); //设置顶部距离父元素的顶部20

        make.left.equalTo(self.view.mas_left).offset(20);

        make.right.equalTo(self.view.mas_right).offset(-20);

        make.height.mas_equalTo(50);  // 可以自动装箱把 int 类型的50 转化为id 类型的50

       // make.height.equalTo(50); // 这里不能写50,要求传入id类型,mas_equalTo才可以把int转化为id

    }];


  

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

    blueView.backgroundColor=[UIColor blueColor];

    self.blueView1=blueView;

    [self.view addSubview: blueView];

    [blueView makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(redView.bottom).equalTo(20);  //blueView相对于redView底部20

        make.right.equalTo(redView.right).equalTo(0);

        make.height.equalTo(redView.height);

        make.width.equalTo(redView.width).multipliedBy(0.5);

    }];


 // 更新约束: 

//    [redView updateConstraints:^(MASConstraintMaker *make) {

//        make.height.equalTo(200);

//    }];



猜你喜欢

转载自blog.csdn.net/dreams_deng/article/details/80655116