masonry快速设置等宽

286 阅读1分钟
#import "ViewController.h"
#import <Masonry/Masonry.h>

@interface ViewController ()

@property (nonatomic,strong) UIView *redView;
@property (nonatomic,strong) UIView *blueView;
@property (nonatomic,strong) UIView *yellowView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.view addSubview:self.redView];
    [self.view addSubview:self.blueView];
    [self.view addSubview:self.yellowView];
    [self p_addMasonry];
}

- (void)p_addMasonry {
    CGFloat padding = 10;
    
    /********  等高   **********/
    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.view).insets(UIEdgeInsetsMake(padding, padding, 0, padding));
        make.bottom.mas_equalTo(self.blueView.mas_top).mas_offset(-padding);
    }];
    [self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view).insets(UIEdgeInsetsMake(0, padding, 0, padding));
        make.bottom.mas_equalTo(self.yellowView.mas_top).mas_offset(-padding);
    }];
    [self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view).insets(UIEdgeInsetsMake(0, padding, padding, padding));
        make.height.mas_equalTo(@[self.blueView, self.redView]);
    }];
    
    /**********  等宽   ***********/
    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(self.view).insets(UIEdgeInsetsMake(padding, padding, padding, 0));
        make.right.mas_equalTo(self.blueView.mas_left).mas_offset(-padding);
    }];
    [self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self.view).insets(UIEdgeInsetsMake(padding, 0, padding, 0));
        make.right.mas_equalTo(self.yellowView.mas_left).mas_offset(-padding);
    }];
    [self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(self.view).insets(UIEdgeInsetsMake(padding, 0, padding, padding));
        make.width.mas_equalTo(@[self.redView, self.blueView]);
    }];
}

- (UIView *)redView {
    if (!_redView) {
        _redView = [[UIView alloc] init];
        _redView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    }
    return _redView;
}

- (UIView *)blueView {
    if (!_blueView) {
        _blueView = [[UIView alloc] init];
        _blueView.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.1];
    }
    return _blueView;
}

- (UIView *)yellowView {
    if (!_yellowView) {
        _yellowView = [[UIView alloc] init];
        _yellowView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.1];
    }
    return _yellowView;
}

@end