Masonry使用

mas_equalTo和equalTo

默认情况下

mas_equalTo有自动包装功能,比如自动将20包为@20

equalTo没有自动包装功能

如果添加下面的宏,那么mas_equal和equalTo就没有区别

#define MAS_SHORTHAND_GLOBALS

注意:这个宏一定要添加到#import "Masonry.h"前面


mas_width和width

默认情况下

width是make对象的一个属性,用来添加宽度约束用的,表示对宽度进行约束

mas_width是一个属性值,用来当做equalTo的参数,表示某个空间的宽度属性

如果添加了下面的宏,mas_width也可以写成width

#define MAS_SHORTHAND

Masonry使用_第1张图片

#import "ViewController.h"
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import 
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIView *blueView = UIView.new;
    blueView.backgroundColor = [UIColor colorWithRed:183 / 255.0 green:211 / 255.0 blue:250 / 255.0 alpha:1];
    
    [self.view addSubview:blueView];
    
    UIView *redView = UIView.new;
    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);
    }];
//    /** 会将以前的所有约束删掉,添加新的约束 */
//    [blueView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        
//    }];
//    
//    /** 将会覆盖以前的某些约束 */
//    [blueView mas_updateConstraints:^(MASConstraintMaker *make) {
//        
//    }];
}

/** 
 *约束的类型:
 1.尺寸: width/height/size
 2.边界: left/leading/right/trailing/top/bottom
 3.中心点: center/centerX/centerY
 4.边界: edges
 */

- (void)test4 {
    
    UIView *blueView = UIView.new;
    blueView.backgroundColor = [UIColor colorWithRed:183 / 255.0 green:211 / 255.0 blue:250 / 255.0 alpha:1];
    
    [self.view addSubview:blueView];
    
    [blueView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.width);
        make.height.equalTo(self.view.height);
        make.centerX.equalTo(self.view.centerX);
        make.centerY.equalTo(self.view.centerY);
    }];
}

- (void)test3 {
    
    UIView *blueView = UIView.new;
    blueView.backgroundColor = [UIColor colorWithRed:183 / 255.0 green:211 / 255.0 blue:250 / 255.0 alpha:1];
    
    [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));
    }];
}

- (void)test2 {
    
    UIView *blueView = UIView.new;
    blueView.backgroundColor = [UIColor colorWithRed:183 / 255.0 green:211 / 255.0 blue:250 / 255.0 alpha:1];
    
    [self.view addSubview:blueView];
    
    /**
     尺寸限制: 父控件一半
     位置: 居中
     */
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.view).multipliedBy(0.5);
        make.center.mas_equalTo(self.view);
    }];
}

- (void)test1 {
    
    UIView *blueView = UIView.new;
    blueView.backgroundColor = [UIColor colorWithRed:183 / 255.0 green:211 / 255.0 blue:250 / 255.0 alpha:1];
    
    [self.view addSubview:blueView];
    
    /**
     尺寸限制: 100 X 100
     位置: 粘着父控件右下角,间距是20
     */
    
    /** 这个方法只会添加新的约束 */
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        /** 在这个block里利用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);
    }];
}


你可能感兴趣的:(项目总结)