Skip to content

Instantly share code, notes, and snippets.

@faimin
Created May 16, 2017 02:54
Show Gist options
  • Select an option

  • Save faimin/e640df6d29bce63c3fdbdc355f09f984 to your computer and use it in GitHub Desktop.

Select an option

Save faimin/e640df6d29bce63c3fdbdc355f09f984 to your computer and use it in GitHub Desktop.
a UIView category for calculateDynamicHeight
- (CGFloat)calculateDynamicHeightWithMaxWidth:(CGFloat)maxWidth {
self.translatesAutoresizingMaskIntoConstraints = NO;
CGFloat viewMaxWidth = maxWidth ? : CGRectGetWidth([UIScreen mainScreen].bounds);
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:viewMaxWidth];
[self addConstraint:widthConstraint];
CGSize fittingSize = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
[self removeConstraint:widthConstraint];
return fittingSize.height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment