Masonry的使用方法

Masonry基本参数

// mas_equalTo 会对参数进行包

//    equalTo  不会对参数进行包装

//    mas_equalTo可以使用

若想使equalTo和 equalTo有等同的效果,可以导入这两个宏

//加上这个宏,mas_equalto,equalto相同

#define MAS_SHORTHAND

//该宏会自动包装

#define MAS_SHORTHAND_GLOBALS


//这个方法只会添加新的约束

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

    }];

    //这个将会将以前的约束删掉,添加新的

    [blueView mas_remakeConstraints:^(MASConstraintMaker *make) {

    }];

    //这个方法将会覆盖以前的某些特定的约束

    [blueView mas_updateConstraints:^(MASConstraintMaker *make) {

    }];



做出这样的效果图可以使用这些方法进行实现,相同的效果不同表现形式。可以有很多表现形式,只列出以下几种。

// ################ Method1############################

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.width.equalTo(@100);

        make.height.equalTo(@100);

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

        make.bottom .equalTo(self.view.mas_bottom).offset(-20);

    }];

    //  ################ Method2############################

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.width.mas_equalTo(100);  // mas_equalTo 自动包装

        make.height.mas_equalTo(100);

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

        make.bottom .equalTo(self.view).offset(-20);

    }];

    //  ################ Method3############################

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.width.height.mas_equalTo(100);  // mas_equalTo

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

        make.bottom .equalTo(self.view).offset(-20);

    }];

// ################ Method4############################

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(CGSizeMake(100, 100));

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

        make.bottom .equalTo(self.view).offset(-20);

    }];

    //  ################ Method5############################

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(CGSizeMake(100, 100));

        make.right.offset(-20); //默认是父控件

        make.bottom.offset(-20);

    }];

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(self.view).multipliedBy(0.5).offset(-50);//按比例计算

        make.right.offset(-20); //默认是父控件

        make.bottom.offset(-20);

    }];



//蓝色

    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];

    CGFloat margin = 20;

    CGFloat height = 40;

    //  ################ Method1############################

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view.left).offset(margin);

        make.right.equalTo(redView.left).offset(-margin);

        make.bottom.equalTo(self.view.bottom).offset(-margin);

        make.height.equalTo(height);

        make.width.equalTo(blueView.width);

    }];

    [redView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(blueView.right).offset(margin);

        make.right.equalTo(self.view.right).offset(-margin);

        make.bottom.equalTo(self.view.bottom).offset(-margin);

        make.height.equalTo(height);

        make.width.equalTo(blueView.width);

    }];

    //  ################ Method2############################

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view.left).offset(margin);

        make.right.equalTo(redView.left).offset(-margin);

        make.bottom.equalTo(self.view.bottom).offset(-margin);

        make.height.equalTo(height);

        make.top.equalTo(redView.top);

        make.bottom.equalTo(redView.bottom);

        make.width .equalTo(redView.width);

    }];

    [redView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.equalTo(self.view.right).offset(-margin);

    }];

你可能感兴趣的:(Masonry的使用方法)