Tuesday, 10 September 2013

Resizing UIScrollView to fit its content… not working

Resizing UIScrollView to fit its content… not working

I have an app that uses a UIScrollView to display content created by
Interface Builder. I add a UITextView to it programmatically because its
content is changeable and I want it to resize. This is done! The problem
comes when resizing the UIScrollView to fit the new UITextView size. I
just can't get it done.. I tried various methods including:
CGRect contentRect = CGRectZero; for (UIView *view in
self.scrollView.subviews) contentRect = CGRectUnion(contentRect,
view.frame); self.scrollView.contentSize = contentRect.size;
I think the problem is because it is built by Interface Builder but the
UIScrollView contains other elements as well so I can't create it
programmatically. I was hoping someone could help me on this.
UPDATE 1: I have both the UIView and the UIScrollView's heights set to
1024 from the Interface Builder.
UPDATE 2 (FIX): I was able to fix this problem by issuing a timer at the
end of the viewDidLoad method like so:
[NSTimer scheduledTimerWithTimeInterval:0.00001 target:self
selector:@selector(resizeScrollView) userInfo:nil repeats:NO]
and then resizing it in the resizeScrollView method here:
-(void)resizeScrollView { CGFloat scrollViewHeight = 0.0f; for (UIView*
view in scrollView.subviews) { scrollViewHeight += view.frame.size.height;
} [scrollView setContentSize:(CGSizeMake(320, scrollViewHeight-50))]; }
Thanks for all the help!
NOTE: This solution was inspired by Maniac One's answer. Thanks!

No comments:

Post a Comment