UIStackView使用(二)

S1)%MG92$LMXJL1@G@{5GOU.png

LabelButton等控件一样,Stack View 也有自己的Intrinsic Size,在未设定约束时亦能确定自己的大小。其依赖于内部subView的大小和space。在subView约束未定时其本身大小同依据于本身Intrinsic Size确定。这是一个嵌套递归的过程。

做为 UIView ,它没有自己的Intrinsic Size, 但可以使用约束来产生, make.width.height.mas_equalTo(100);
注意不能设置位置相关约束,否则会变成绝对定位而失去栈布局流的特性。

UIStackView 刚好相反,如果只设置位置约束,它就是弹性盒子,Intrinsic Size 由内容决定,一旦设置了大小,就变成了刚体盒子,内容的大小与位置由盒子大小来决定。

  • 设置 UILayoutConstraintAxisHorizontal 相当于 子元素都成为 row
  • 设置 UILayoutConstraintAxisVertical 相当于子元素都成为 col

使用 UIStackView 嵌套布局cell

//
//  ESSNSTableViewCell.m
//  stackView
//
//  Created by ldhonline on 2018/10/14.
//  Copyright © 2018年 aidoutu.com. All rights reserved.
//

#import "ESSNSTableViewCell.h"
#import "masonry.h"

@implementation ESSNSTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style
                reuseIdentifier:reuseIdentifier];
    if(self){
        [self es_addSubviews];
        self.selectionStyle = UITableViewCellEditingStyleNone;
        self.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return self;
}

- (void)es_addSubviews{
    
    UIStackView *main = [self rowsWithMargin:UIEdgeInsetsMake(15, 15, 15, 15) spacing:10];
    [self.contentView addSubview:main];
    [main mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.offset(0);
        make.right.offset(0);
        make.bottom.offset(0);
    }];

    UIStackView *headBar = [self colsWithMargin:UIEdgeInsetsMake(0, 0, 0, 0) spacing:10];
    [main addArrangedSubview:headBar];
    
    UIView *icon = [self box];
    icon.layer.cornerRadius = 18;
    
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(36);
    }];
    
    [headBar addArrangedSubview:icon];
    
    UIStackView *userInfo = [self rowsWithMargin:UIEdgeInsetsMake(0, 0, 0, 0) spacing:5];
    [headBar addArrangedSubview:userInfo];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:17];
    label.text = @"用户小宝";
    label.textColor = [UIColor blueColor];
    label.textAlignment = NSTextAlignmentCenter;
    [userInfo addArrangedSubview:label];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.font = [UIFont systemFontOfSize:13];
    label2.text = @"12:04 北京";
    label2.textColor = [UIColor colorWithWhite:0.7 alpha:1];
    label2.textAlignment = NSTextAlignmentCenter;
    [userInfo addArrangedSubview:label2];
    
    UILabel *label3 = [[UILabel alloc] init];
    label3.font = [UIFont systemFontOfSize:18];
    label3.text = @"之前来中国席卷票房的《碟中谍6》,让56岁依然亲身上阵各种高危动作戏的老帅哥汤姆?克鲁斯又火了一把。年过半百,汤帅依然身材干练,飞车、开直升机丝毫不怵。";
    label3.textColor = [UIColor colorWithWhite:0.1 alpha:1];
    label3.numberOfLines = 0;
    [main addArrangedSubview:label3];
    
    UIStackView *imagebox = [self colsWithMargin:UIEdgeInsetsMake(0, 0, 0, 0) spacing:5];

    int cout = ceil(rand()%3) + 1;
    
    CGFloat w = floor(([UIScreen mainScreen].bounds.size.width - 30 - 10)/3);
    
    for (int i = 0; i

你可能感兴趣的:(UIStackView使用(二))