Skip to content

Instantly share code, notes, and snippets.

@sberrevoets
Last active August 29, 2015 13:56
Show Gist options
  • Save sberrevoets/9053689 to your computer and use it in GitHub Desktop.
Save sberrevoets/9053689 to your computer and use it in GitHub Desktop.
UIView subclass that has a valid intrinsicContentSize based on its subviews' frames
- (CGSize)intrinsicContentSize {
__block CGFloat minX = CGFLOAT_MAX;
__block CGFloat maxX = CGFLOAT_MIN;
__block CGFloat minY = CGFLOAT_MAX;
__block CGFloat maxY = CGFLOAT_MIN;
[[self subviews] enumerateObjectsUsingBlock:^(UIView *subview, NSUInteger idx, BOOL *stop) {
// Ignore _UILayoutGuide
if (![subview conformsToProtocol:@protocol(UILayoutSupport)]) {
// We could use CGRectGet(Min|Max)(X|Y) using the subview.frame, but subview.frame is undefined when a transform is applied. This calculation is transform-friendly.
minX = MIN(minX, subview.center.x - CGRectGetWidth(subview.bounds) / 2);
maxX = MAX(maxX, subview.center.x + CGRectGetWidth(subview.bounds) / 2);
minY = MIN(minY, subview.center.y - CGRectGetHeight(subview.bounds) / 2);
maxY = MAX(maxY, subview.center.y + CGRectGetHeight(subview.bounds) / 2);
}
}];
// If minX is still set to CGFLOAT_MAX, there were no subviews, so just return super's intrinsicContentSize
if (minX == CGFLOAT_MAX)
return [super intrinsicContentSize];
return CGSizeMake(maxX - minX, maxY - minY);
}
- (void)layoutSubviews {
[super layoutSubviews];
[self invalidateIntrinsicContentSize];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment