Masonry 使用复习总结

title: Masonry 使用复习总结
date: 2015-08-23 11:07:53
categories: IOS

tags: Masonry

小小程序猿
我的博客:http://daycoding.com

为了解决ios 原生写约束太痛苦这个问题
github:Masonry

安装

使用Pod组织安装masonry

platform :ios, '6.0'
pod 'Masonry'

注意

  • 如果配合xib使用需要将“Use Auto Layout”和“Use Size Classes”这两个选项去掉,要不然控制台会报错
  • 要先添加view,在添加约束
  • 最好在约束中设置控件大小

经常用到的约束

设置控件宽高

   UIView* red = [[UIView alloc]init];
    red.backgroundColor = [UIColor redColor];
    [self.view addSubview:red];
    //要先添加view 之后才能添加约束
    [red mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(100, 100));
        //如果使用eqaulto时,设置数值前需要加@
        //mas_equalTo则不用
        make.height.equalTo(@100);
        make.width.mas_equalTo(100);
    }];
    

运行结果:
运行结果

等同于其他控件宽高
```
UIView* red = [[UIView alloc]init];
red.backgroundColor = [UIColor redColor];
[self.view addSubview:red];
[red mas_makeConstraints:MASConstraintMaker *make {
// make.size.mas_equalTo(CGSizeMake(100, 100));
//如果使用eqaulto时,设置数值前需要加@
//mas_equalTo则不用
make.height.equalTo(@100);
make.width.mas_equalTo(100);
}];

UIView* blue= [[UIView alloc] init];
blue.backgroundColor = [UIColor blueColor];
[self.view addSubview:blue];
[blue mas_makeConstraints:^(MASConstraintMaker *make) {

// make.size.equalTo(red);
// 等价于
make.width.equalTo(red);
make.height.equalTo(red);
//这个用来设置距离红方块的距离
make.left.equalTo(red.mas_right).offset(10);
}];
```
结果:
enter image description here

设置边距margin

top和left的边距设置

UIView* red = [[UIView alloc]init];
red.backgroundColor = [UIColor redColor];
[self.view addSubview:red];
[red mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@100);
    make.width.mas_equalTo(100);
}];

UIView* blue= [[UIView alloc] init];
blue.backgroundColor = [UIColor blueColor];
[self.view addSubview:blue];
[blue mas_makeConstraints:^(MASConstraintMaker *make) {

    make.width.equalTo(red);
    make.height.equalTo(red);
    make.left.equalTo(red.mas_right).offset(100);
    make.top.equalTo(red.mas_bottom).offset(100);

}];

运行结果:

右方和下方的边距设置
```
UIView* blue= [[UIView alloc] init];
blue.backgroundColor = [UIColor blueColor];
[self.view addSubview:blue];
[blue mas_makeConstraints:MASConstraintMaker *make {

    make.height.equalTo(@100);
    make.width.mas_equalTo(100);
    make.right.equalTo(self.view.mas_right).offset(-50);
    make.bottom.equalTo(self.view.mas_bottom).offset(-20);

}];

运行结果:
![](http://7xk16d.com1.z0.glb.clouddn.com/15-6-30/30251805.jpg)

**注意:在设置边距的时候上、左边距的值是正数,右、下的边距值是负数**
### 设置左右对齐
 UIView* red = [[UIView alloc]init];
red.backgroundColor = [UIColor redColor];
[self.view addSubview:red];
[red mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@100);
    make.width.mas_equalTo(200);
    make.left.equalTo(self.view.mas_left).offset(30);
}];
UIView* blue= [[UIView alloc] init];
blue.backgroundColor = [UIColor blueColor];
[self.view addSubview:blue];
[blue mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@100);
    make.width.mas_equalTo(100);
    //让蓝方块在红方块下方
    make.top.equalTo(red.mas_bottom).offset(10);
    //设置左对齐
    make.left.equalTo(red.mas_left);
}];


  运行结果:
![](http://7xk16d.com1.z0.glb.clouddn.com/15-6-30/39647700.jpg)                     ![](http://7xk16d.com1.z0.glb.clouddn.com/15-6-30/60579122.jpg)
###撑满剩余空间###

UIView* red = [[UIView alloc]init];
red.backgroundColor = [UIColor redColor];
[self.view addSubview:red];
[red mas_makeConstraints:MASConstraintMaker *make {
make.height.equalTo(@50);
make.width.mas_equalTo(50);
make.left.equalTo(self.view.mas_left).offset(10);
make.top.equalTo(self.view.mas_top).offset(20);
}];

UIView* blue= [[UIView alloc] init];
blue.backgroundColor = [UIColor blueColor];
[self.view addSubview:blue];
[blue mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(red);
    make.centerY.equalTo(red.mas_centerY);
    make.left.equalTo(red.mas_right).offset(10);
    make.right.equalTo(self.view.mas_right).offset(-10);

}];
结果:
![运行结果](http://7xk16d.com1.z0.glb.clouddn.com/15-7-1/67504229.jpg)


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值