How to set up the frame of UITextView in a UIScrollview

Go To StackoverFlow.com

1

i'm struggling to get UIScrollView working.

I have a UIScrollView with a UIImageView and UITextView as it's subviews.

The UIScrollView, UIImageView and UITextView are hooked up to my controller.

When I use this line of code in viewDidLoad:

self.scrollView.contentSize = CGSizeMake(320, 1000);

the scrolling works. However, when scrolling down, if the UITextView contains lots of text, the bottom part of the text is cut of. this is what is displayed after scrolling down

I assume that I must adjust the frame of the UITextView. When I add these three lines in viewDidLoad:

CGRect frame = self.bioText.frame;
frame.size = self.bioText.contentSize; 
self.bioText.frame = frame;

the text is cut off even more.

even less text

A few questions:

  • how do i properly can adjust the UITextView so that all the text is displayed when scrolling.
  • is there a way to set the scrollview contentSize to the sum of all it's subviews (instead of having to hard code it like I have now)
2012-04-04 17:55
by murze


1

You do not need to adjust the contentSize of the textView, it automatically adjusts according to its text

If you want to have the contentSize of your UIScrollView adjust according to its subviews and all of your subviews are vertically you can use this

CGFloat height = 0
for(UIView* sub in myScrollView.subviews)
height += sub.frame.size.height

myScrollView.contentSize = CGSizeMake(self.contentSize.width, height)
2012-04-04 18:16
by Otium


0

Today I've just solved this problem. You can set the textView' frame in viewDidLayoutSubviews. This is the code in my App:

- (void)viewDidLayoutSubviews {
    CGRect rect = self.textView.frame;
    rect.size.height = self.textView.contentSize.height;
    self.textView.frame = rect;
}

Hope this can help you.

2014-03-30 01:59
by wubincen
Ads