masonry框架的使用

来源:互联网 发布:数控编程的内容和步骤 编辑:程序博客网 时间:2024/05/17 22:56


1)框架写法的四步优化


#import "ViewController.h"

#import "Masonry.h"

@interfaceViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];



    UIView *blueView = [[UIViewalloc]init];

    blueView.backgroundColor = [UIColorblueColor];

    [self.viewaddSubview:blueView];

    

    UIView *redView = [[UIViewalloc]init];

    redView.backgroundColor = [UIColorredColor];

    [self.viewaddSubview: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"

@interfaceViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    UIView *blueView = [[UIViewalloc]init];

    blueView.backgroundColor = [UIColorblueColor];

    [self.viewaddSubview: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 {

    [superviewDidLoad];

    UIView *blueView = [[UIViewalloc]init];

    blueView.backgroundColor = [UIColorblueColor];

    [self.viewaddSubview:blueView];

    

    UIView *redView = [[UIViewalloc]init];

    redView.backgroundColor = [UIColorredColor];

    [self.viewaddSubview: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


0 0
原创粉丝点击