IOS--通过宽度算高度

//通过宽度算高度
- (CGFloat)sugguestHeightForWidth:(CGFloat)width
{
    NSLayoutConstraint *tempWidthConstraint =
    [NSLayoutConstraint constraintWithItem:self
                                 attribute:NSLayoutAttributeWidth
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:nil
                                 attribute:NSLayoutAttributeNotAnAttribute
                                multiplier:1.0
                                  constant:width];
    [self addConstraint:tempWidthConstraint];
    CGSize fittingSize = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    [self removeConstraint:tempWidthConstraint];
    return fittingSize.height;
}
1 CGFloat height = [addShelfView sugguestHeightForWidth:self.view.hm_width];
2         addShelfView.hm_height = height;
原文地址:https://www.cnblogs.com/qiyiyifan/p/7596581.html