iOS UIStackView

247 阅读1分钟
@property(nonatomic,strong)UIView *redView;
@property(nonatomic,strong)UIView *blueView;
@property(nonatomic,strong)UIView *orangeView;

@property(nonatomic,strong)UIStackView *mainStackView;
- (UIStackView *)mainStackView
{
    if (!_mainStackView) {
        _mainStackView = [[UIStackView alloc] init];
        _mainStackView.backgroundColor = [UIColor grayColor];
        _mainStackView.spacing = 10.0; // 间距
        _mainStackView.axis = UILayoutConstraintAxisHorizontal;  //水平布局
        _mainStackView.alignment = UIStackViewAlignmentLastBaseline; // 底部对齐
        _mainStackView.distribution = UIStackViewDistributionFillEqually;//等间距
    }
    return _mainStackView;
}

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

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

- (UIView *)orangeView
{
    if (!_orangeView) {
        _orangeView = [[UIView alloc] init];
        _orangeView.backgroundColor = [UIColor orangeColor];
    }
    return _orangeView;
}
[self.view addSubview:self.mainStackView];
    [self.mainStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(100.0);
        make.left.right.equalTo(self.view).offset(0.0);
        make.height.mas_equalTo(100.0);
    }];
    
    [self.mainStackView addArrangedSubview:self.redView];
    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30.0);
    }];
    [self.mainStackView addArrangedSubview:self.blueView];
    [self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40.0);
    }];
    [self.mainStackView addArrangedSubview:self.orangeView];
    [self.orangeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50.0);
    }];