masonry的等间距

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

@interface ViewController ()

@property (nonatomic,strong) UIView *baseView;
@property (nonatomic,strong) NSMutableArray *masonryViewArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.baseView];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    //    [self test_masonry_horizontal_fixItemWidth];//水平排列
    [self test_masonry_vertical_fixItemWidth];//垂直排列
}

- (void)test_masonry_horizontal_fixItemWidth {//水平排列
    // 实现masonry水平固定控件宽度方法
    [self.masonryViewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:80 leadSpacing:10 tailSpacing:10];
    // 设置array的垂直方向的约束
    [self.masonryViewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(150);
        make.height.mas_equalTo(80);
    }];
}

- (void)test_masonry_vertical_fixItemWidth {//垂直排列
    // 实现masonry垂直方向固定控件高度方法
    [self.masonryViewArray mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:80 leadSpacing:10 tailSpacing:10];
    // 设置array的水平方向的约束
    [self.masonryViewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(150);
        make.width.mas_equalTo(80);
    }];
}

- (NSMutableArray *)masonryViewArray {
    if (!_masonryViewArray) {
        _masonryViewArray = [NSMutableArray array];
        for (NSInteger i = 0; i < 4; i ++) {
            UIView *colorView = [[UIView alloc] init];
            colorView.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.6];
            [self.baseView addSubview:colorView];
            [_masonryViewArray addObject:colorView];
        }
    }
    return _masonryViewArray;
}

- (UIView *)baseView {
    if (!_baseView) {
        _baseView = [[UIView alloc] init];
        _baseView.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width);
        _baseView.center = self.view.center;
        _baseView.backgroundColor = [UIColor grayColor];
    }
    return _baseView;
}

@end

#import "ViewController.h"
#import <Masonry/Masonry.h>

@interface ViewController ()

@property (nonatomic,strong) UIView *baseView;
@property (nonatomic,strong) NSMutableArray *yellowColorViewArray;
@property (nonatomic,strong) NSMutableArray *blueColorViewArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.baseView];
    
    //在红色View里面放三个正方形View, 等间距为10
    NSInteger padding = 10;
    [self.yellowColorViewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:padding leadSpacing:padding tailSpacing:padding];
    [self.yellowColorViewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.baseView).mas_offset(10);
        make.height.mas_equalTo(((UIView *)self.yellowColorViewArray.lastObject).mas_width);
    }];
    
    //在红色View里面放三个正方形蓝色View, 宽度均为30, 间隙一样大
    CGFloat padding2 = (300 - 3 * 30) / 4;
    [self.blueColorViewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:30 leadSpacing:padding2 tailSpacing:padding2];
    [self.blueColorViewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.baseView);
        make.height.mas_equalTo(((UIView *)self.blueColorViewArray.firstObject).mas_width);
    }];
}

- (UIView *)baseView {
    if (!_baseView) {
        _baseView = [[UIView alloc] init];
        _baseView.bounds = CGRectMake(0, 0, 300, 300);
        _baseView.center = self.view.center;
        _baseView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    }
    return _baseView;
}

- (NSMutableArray *)yellowColorViewArray {
    if (!_yellowColorViewArray) {
        _yellowColorViewArray = [NSMutableArray array];
        for (NSInteger i = 0; i < 3; i ++) {
            UIView *yellowView = [[UIView alloc] init];
            yellowView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.5];
            [self.baseView addSubview:yellowView];
            [_yellowColorViewArray addObject:yellowView];
        }
    }
    return _yellowColorViewArray;
}

- (NSMutableArray *)blueColorViewArray {
    if (!_blueColorViewArray) {
        _blueColorViewArray = [NSMutableArray array];
        for (NSInteger i = 0; i < 3; i ++) {
            UIView *blueView = [[UIView alloc] init];
            blueView.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.3];
            [self.baseView addSubview:blueView];
            [_blueColorViewArray addObject:blueView];
        }
    }
    return _blueColorViewArray;
}

@end