masnory:
// 图片
// [self.view addSubview:self.picturesView];
//
// if (self.picturesView.superview) {
// [self.picturesView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(headLine.mas_bottom).offset(15.f);
// make.left.equalTo(self.view.mas_left).offset(20.f);
// make.right.equalTo(self.view.mas_right).offset(-20.f);
// make.height.mas_equalTo(self.picturesView.maxHeight);
// }];
// }
stackView:
@property (nonatomic, strong) NSLayoutConstraint *layoutConstraint;
懒加载
-(UIStackView *)stackView{
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisVertical;
_stackView.distribution = UIStackViewDistributionEqualSpacing;
_stackView.alignment = UIStackViewAlignmentFill;
_stackView.spacing=10;
}
return _stackView;
}
添加视图:
self.layoutConstraint = [self.picturesView.heightAnchor constraintEqualToConstant:self.picturesView.maxHeight];
self.layoutConstraint.active = true;
[self.picturesView.widthAnchor constraintEqualToConstant:DDViewUtils.viewControllerWidth].active = true;
[self.stackView addArrangedSubview:self.picturesView];
[self.stackView layoutIfNeeded];
合适时机更新高度:
self.layoutConstraint.constant = self.picturesView.maxHeight;
[self.view setNeedsUpdateConstraints];
[self.view layoutIfNeeded];