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;

 

posted on 2017-09-26 14:20  麦芽呀~  阅读(459)  评论(0编辑  收藏  举报