https://www.jianshu.com/p/19fbf3ee2840
https://www.cnblogs.com/bokeyuanlibin/p/5693575.html
https://www.cnblogs.com/breezemist/p/5776552.html
对于宽度不一致的SubViews ,需要用约束控制其自定义的宽度or高度 ,可以用masonry
例子:
UIStackView *stackContainerView = [[UIStackView alloc] init];
self.stackContainerView = stackContainerView;
stackContainerView.axis = UILayoutConstraintAxisHorizontal;
stackContainerView.spacing = 4 ;
stackContainerView.alignment = UIStackViewAlignmentFill;
stackContainerView.distribution = UIStackViewDistributionEqualSpacing;
[self.contentView addSubview:stackContainerView];
CGFloat stackViewWidth = 0;
// 创建标签view
for (int i = 0; i < self.recommendModel.marketing.count; i ++) {
NSString *tagKey = self.recommendModel.marketing[i];
MZTagView *tagview = [[MZTagView alloc] initWithTagKey:tagKey];
[stackContainerView addArrangedSubview:tagview];
[tagview mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(stackContainerView);
make.width.mas_equalTo(tagview.tagViewWidth);
make.height.mas_equalTo( 20 *MZ_RATE);
}];
stackViewWidth += tagview.tagViewWidth;
}
stackViewWidth += (self.recommendModel.marketing.count-1)*4;
[stackContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.avatorView);
make.width.mas_equalTo(stackViewWidth);
make.height.mas_equalTo(20 );
make.right.equalTo(self.contentView).offset(-16);
}];