iOS之常用第三方库的实用iOS开发iOS Developer

Masonry

2016-08-22  本文已影响269人  阿龍飛

1、常用方法:
这个方法只会添加新的约束

[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
}];

这个方法会将以前的所有约束删掉,添加新的约束

[blueView mas_remakeConstraints:^(MASConstraintMaker *make) {  
}];

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

[blueView mas_updateConstraints:^(MASConstraintMaker *make) {    
}]; 

2、约束类型
尺寸:
width(宽)\height(高)\size(大小)\

       // 宽度约束
        make.width.mas_equalTo(100);
        // 高度约束
        make.height.mas_equalTo(100);
        
        //  大小约束(与上面两句等价)
        make.size.mas_equalTo(CGSizeMake(100, 100));

边界:
left\leading(左边界)\right\trailing(右边界)\top(顶部边界)\bottom(底部边界)

       // 左边(leading类似)
        make.left.mas_equalTo(self.view).offset(50); 
       // 右边(trailing类似)
        make.right.equalTo(self.view).offset(-20);
        // 顶部
        make.top.equalTo(self.view).offset(20);
        // 底部
        make.bottom.mas_equalTo(self.view).offset(-50);

中心点:
center\centerX\centerY

    // 居中(水平+垂直)
    // 尺寸是父控件的一半
    [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);
    }];

内边距实现边界约束:

// UIEdgeInsets 内边距
make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));

3、mas_前缀修饰与不修饰的区别
mas_equalTo和equalTo
默认情况下:
mas_equalTo有自动包装功能,比如自动将20包装为@20
equalTo没有自动包装功能
mas_equalTo的功能强于 > equalTo,可以一直使用mas_equalTo




#import "ViewController.h"
#import "Masonry/Masonry.h"
@interface ViewController ()
@property (nonatomic, strong) UIView *A;
@property (nonatomic, strong) UIView *B;
@property (nonatomic, strong) UIView *C;
@property (nonatomic, strong) UIView *D;
@property (nonatomic, strong) UIView *E;
@property (nonatomic, strong) UIView *F;
@property (nonatomic, strong) UIView *G;

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];


    _A = [[UIView alloc]init];
    _A.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_A];
    
    _B = [[UIView alloc]init];
    _B.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_B];
    
    _C = [[UIView alloc]init];
    _C.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_C];
    
    _D = [[UIView alloc]init];
    _D.backgroundColor = [UIColor yellowColor];
    [_C addSubview:_D];
    
    _E = [[UIView alloc]init];
    _E.backgroundColor = [UIColor blueColor];
    [_C addSubview:_E];
    
    _F = [[UIView alloc]init];
    _F.backgroundColor = [UIColor redColor];
    [_C addSubview:_F];
    
    
    
    [_A mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(20);
        make.height.mas_equalTo(150);
        make.right.equalTo(_B.mas_left).mas_offset(-20);
        make.width.mas_equalTo(_B);
    }];
    

    [_B mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(150);
        make.top.mas_equalTo(20);
        make.left.equalTo(_A.mas_right).mas_offset(20);
        make.width.mas_equalTo(_A);
    }];

    [_C mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_A.mas_bottom).mas_offset(20);
        make.height.equalTo(_A);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    

    [_D mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_C).mas_offset(10);
        make.left.equalTo(_C).mas_offset(10);
        make.right.equalTo(_E.mas_left).mas_offset(-20);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(_E);
    }];
    
    [_E mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_C).offset(10);
        make.right.equalTo(_C).offset(-10);
        make.left.equalTo(_D.mas_right).mas_offset(20);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(_D);
    }];
    
    [_F mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_D.mas_bottom).mas_offset(10);
        make.right.equalTo(_C).mas_offset(-10);
        make.left.equalTo(_C).mas_offset(10);
        make.bottom.equalTo(_C).mas_offset(-10);
    }];
}

@end
2389367-ad9b97bc40d84c36.png
上一篇下一篇

猜你喜欢

热点阅读