masonry框架的使用


1)框架写法的四步优化


#import "ViewController.h"

#import "Masonry.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];



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

    blueView.backgroundColor = [UIColor blueColor];

    [self.view addSubview:blueView];

    

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

    redView.backgroundColor = [UIColor redColor];

    [self.view addSubview:redView];

    


    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        

//        make.top.mas_equalTo(self.view.mas_top).multipliedBy(1).mas_offset(20);

//        make.left.mas_equalTo(self.view.mas_left).multipliedBy(1).mas_offset(20);

//

//        make.right.mas_equalTo(self.view.mas_right).multipliedBy(1).mas_offset(-20);

//        make.bottom.mas_equalTo(self.view.mas_bottom).multipliedBy(1).mas_offset(-20);

        

//        the first optmizi

//        make.top.mas_equalTo(self.view.mas_top).mas_offset(20);

//        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);

//        make.right.mas_equalTo(self.view.mas_right).mas_offset(-20);

//        make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-20);

        

        //the second optmizi

        

        make.top.mas_equalTo(self.view).mas_offset(20);

        make.left.mas_equalTo(self.view).mas_offset(20);

        make.right.mas_equalTo(self.view).mas_offset(-20);

        make.bottom.mas_equalTo(self.view).mas_offset(-20);

        

        //the third optmizi

        

//        make.top.left.mas_equalTo(self.view).mas_equalTo(20);

//        make.right.bottom.mas_equalTo(self.view).mas_offset(-20);

        

        //the final optmizi

//        make.edges.mas_offset(UIEdgeInsetsMake(20, 20, 20, 20));


    }];


2)约束的更新和移除

#import "ViewController.h"

#import "Masonry.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

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

    blueView.backgroundColor = [UIColor blueColor];

    [self.view addSubview:blueView];

    

    //make

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        

        make.edges.mas_equalTo(UIEdgeInsetsMake(20, 20, 20, 20));

    }];

    //replace

    [blueView mas_updateConstraints:^(MASConstraintMaker *make) {

        make.edges.mas_equalTo(UIEdgeInsetsMake(50, 50, 50, 50));

    }];

    //remove

    [blueView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.edges.mas_equalTo(UIEdgeInsetsMake(100, 100, 100, 100));

    }];

}


@end


3)案例红色view是蓝色view宽度的一半

#import "ViewController.h"

#import "Masonry.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

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

    blueView.backgroundColor = [UIColor blueColor];

    [self.view addSubview:blueView];

    

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

    redView.backgroundColor = [UIColor redColor];

    [self.view addSubview:redView];

    


    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.left.mas_equalTo(self.view).mas_offset(20);

        make.right.mas_equalTo(self.view).mas_offset(-20);

        make.height.mas_offset(100);

    }];

    [redView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(blueView.mas_bottom).mas_offset(50);

        make.trailing.mas_equalTo(blueView);

        make.height.mas_equalTo(blueView);

        make.width.mas_equalTo(blueView).multipliedBy(0.5);

    }];

}


@end


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值