Skip to content

Instantly share code, notes, and snippets.

@lexrus
Created May 14, 2013 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lexrus/5574347 to your computer and use it in GitHub Desktop.
Save lexrus/5574347 to your computer and use it in GitHub Desktop.
LTScrollView.m set contentSize that fit the contents of UIScrollView
#import "LTScrollView.h"
@implementation LTScrollView
- (void)layoutSubviews
{
CGFloat perfectContentWidth = 0.0f;
CGFloat perfectContentHeight = 0.0f;
BOOL isHorizontalScrollIndicatorVisible = self.showsHorizontalScrollIndicator;
BOOL isVerticalScrollIndicatorVisible = self.showsVerticalScrollIndicator;
self.showsHorizontalScrollIndicator = self.showsVerticalScrollIndicator = NO;
for (UIView *view in self.subviews) {
if (!view.hidden) {
CGFloat x = view.frame.origin.x;
CGFloat y = view.frame.origin.y;
CGFloat w = view.frame.size.width;
CGFloat h = view.frame.size.height;
if (x + w > perfectContentWidth) {
perfectContentWidth = x + w;
}
if (y + h > perfectContentHeight) {
perfectContentHeight = y + h;
}
}
}
self.showsHorizontalScrollIndicator = isHorizontalScrollIndicatorVisible;
self.showsVerticalScrollIndicator = isVerticalScrollIndicatorVisible;
[self setContentSize:CGSizeMake(perfectContentWidth, perfectContentHeight)];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment