源码-0203-Masonry

Masonry

make.attr.equalto.+{ @[ , , , ];    number ->@20;      label / label.mas_left(控件,控件属性) |@(self.button.size.with) }

约束是可以连写的:make.right.equalTo(superview.mas_right).with.offset(-padding.right);

make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5,10,15,20));

prioritize 优先级

make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
make.top.equalTo(label.mas_top).with.priority(600);

 导入masonry插件(拖入文件夹)

删除重复出现的info.plis文件

 

 

//
//  ViewController.m
//  03-Masonry使用
#import "ViewController.h"
//只要添加了了这个宏,就不用mas_前缀
#define MAS_SHORTHAND
//只要添加了这宏,equalTo就等价于mas_equalTo
#define MAS_SHORTHAND_GLOBALS

#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];
    
    // 添加约束
    CGFloat margin = 20;
    CGFloat height = 50;
    [blueView 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 makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.right).offset(-margin);
    }];
}

- (void)test4
{
    // 蓝色控件
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    // 添加约束
    [blueView makeConstraints:^(MASConstraintMaker *make) {
        //        make.width.equalTo(self.view.width).multipliedBy(0.5);
        //        make.height.equalTo(self.view.height).multipliedBy(0.5).offset(-100);
        make.width.equalTo(100);
        make.height.equalTo(100);
        make.centerX.equalTo(self.view.centerX);
        make.centerY.equalTo(self.view.centerY);
    }];
}

- (void)test3
{
    // 蓝色控件
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    // 距离父控件四周都是50间距
    //    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(self.view.mas_left).offset(50);
    //        make.right.mas_equalTo(self.view.mas_right).offset(-50);
    //        make.top.mas_equalTo(self.view.mas_top).offset(50);
    //        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-50);
    //    }];
    //    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(self.view).offset(50);
    //        make.right.mas_equalTo(self.view).offset(-50);
    //        make.top.mas_equalTo(self.view).offset(50);
    //        make.bottom.mas_equalTo(self.view).offset(-50);
    //    }];
    //    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.offset(50);
    //        make.right.offset(-50);
    //        make.top.offset(50);
    //        make.bottom.offset(-50);
    //    }];
    //    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.top.offset(50);
    //        make.right.bottom.offset(-50);
    //    }];
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50)); make.center.mas_equalTo(self.view).insets(UIEdgeInsetsZero ); }]; } - (void)test2 { // 蓝色控件 UIView *blueView = [[UIView alloc] init]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; // 居中(水平+垂直) // 尺寸是父控件的一半 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(self.view).multipliedBy(0.5); make.center.mas_equalTo(self.view); // make.centerX.mas_equalTo(self.view); // make.centerY.mas_equalTo(self.view); }]; } - (void)test1 { // 蓝色控件 UIView *blueView = [[UIView alloc] init]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; // 尺寸限制:100x100 // 位置:粘着父控件右下角,间距是20 // 这个方法只会添加新的约束 // [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // // 宽度约束 // make.width.equalTo(@100); // // 高度约束 // make.height.equalTo(@100); // // 右边 // make.right.equalTo(self.view.mas_right).offset(-20); // // 顶部 // make.top.equalTo(self.view.mas_top).offset(20); // }]; // [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // // 宽度约束 // make.width.mas_equalTo(100); // // 高度约束 // make.height.mas_equalTo(100); // // 右边 // make.right.equalTo(self.view).offset(-20); // // 顶部 // make.top.equalTo(self.view).offset(20); // }]; // [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // // 宽度高度约束 // make.width.height.mas_equalTo(100); // // 右边 // make.right.equalTo(self.view).offset(-20); // // 顶部 // make.top.equalTo(self.view).offset(20); // }]; // [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // // 宽度高度约束 //// make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(100, 100)]); //// make.size.mas_equalTo(CGSizeMake(100, 100)); // make.size.mas_equalTo(100); // // 右边 // make.right.equalTo(self.view).offset(-20); // // 顶部 // make.top.equalTo(self.view).offset(20); // }]; [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // 宽度高度约束 make.height.mas_equalTo(self.view).multipliedBy(0.5).offset(-50); // 右边 make.right.mas_equalTo(self.view).offset(-20); // make.right.offset(-20); // 顶部 make.top.mas_equalTo(self.view).offset(20); // make.top.offset(20); }]; /** mas_equalTo:这个方法会对参数进行包装 equalTo:这个方法不会对参数进行包装 mas_equalTo的功能强于 > equalTo */ } /** 约束的类型: 1.尺寸:width\height\size 2.边界:left\leading\right\trailing\top\bottom 3.中心点:center\centerX\centerY 4.边界:edges */ /** // 这个方法会将以前的所有约束删掉,添加新的约束 [blueView mas_remakeConstraints:^(MASConstraintMaker *make) { }]; // 这个方法将会覆盖以前的某些特定的约束 [blueView mas_updateConstraints:^(MASConstraintMaker *make) { }]; */ @end

 

 

 

 

 

 

 

 
posted @ 2017-02-21 12:28  laugh  阅读(243)  评论(0编辑  收藏  举报