Calculate the contentsize of scrollview

You could try to use the scrollview'ers ContentSize. It worked for me and I had the same problem with the control using dynamic content.

    // Calculate scroll view size
float sizeOfContent = 0;
int i;
for (i = 0; i < [myScrollView.subviews count]; i++) {
    UIView *view =[myScrollView.subviews objectAtIndex:i];
    sizeOfContent += view.frame.size.height;
}

// Set content size for scroll view
myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width, sizeOfContent);

I do this in the method called viewWillAppear in the controller for the view that holds the scrollview. It is the last thing i do before calling the viewDidLoad on the super.

Hope it will solve your problem.

//hannes


Correct shorter example:

float hgt=0; for (UIView *view in scrollView1.subviews) hgt+=view.frame.size.height;

[scrollView1 setContentSize:CGSizeMake(scrollView1.frame.size.width,hgt)];

Note that this only sums heights, e.g. if there are two subviews side by side their heights with both be added, making the sum greater than it should be. Also, if there are vertical gaps between the subviews, the sum will be less than it should be. Wrong height confuses scrollRectToVisible, giving random scroll positions :)

This loop is working and tested:

float thisy,maxy=0;for (UIView *view in scrollView1.subviews) {
    thisy=view.frame.origin.y+view.frame.size.height; maxy=(thisy>maxy) ? thisy : maxy;
}