Youssef Moawad
Youssef Moawad

Reputation: 2866

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!

Upvotes: 0

Views: 1740

Answers (2)

Maniac One
Maniac One

Reputation: 589

change scrollview.contentSize according to your program requirement in 'viewDidScroll' delegate

  • (void)scrollViewDidScroll:(UIScrollView *)sender { } if you are getting new values for textfield while scrolling

Upvotes: 0

Yogi
Yogi

Reputation: 3588

scrollView.contentsize=textview.size

Upvotes: 1

Related Questions