views:

182

answers:

2

Is there a way to size a make a UIScrollView auto adujust to the height (or width) of the content it's scrolling?

something like:

[scrollView setContentSize:(CGSizeMake(320, content.height))];

Thanks

A: 

it depends on the content really : content.frame.height might give you what you want ? Depends if content is a single thing, or a collection of things.

Andiih
It's a collection of things
jwerre
A: 

UIScrollView doesn't know the height of its content automatically. You must calculate the height and width for yourself

Do it with something like

CGFloat scrollViewHeight = 0.0f;
for (UIView* view in scrollView.subviews)
{
   scrollViewHeight += view.frame.size.height;
}

[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];

But this only work if the views are one below the other. If you have a view next to each other you only have to add the height of one if you don't want to set the content of the scroller larger than it really is.

Espuz
Do you think something like this would work as well?CGFloat scrollViewHeight = scrollView.contentSize.height;[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];BTW, don't forget to ask for the size then height.scrollViewHeight += view.frame.size.height
jwerre
Initializing the scrollViewHeight to the height makes the scroller larger than its content. If you do this don't add the height of the views visible on the initial height of the scroller content. Although maybe you need an initial gap or something else.
Espuz