Setting the scroll view's offset - a bug?


I ran the code from the book and it was just right. However, I then tried to make the reallyBigRect 4 times larger than the whole window (rather than 2) and when I ran it the text was no longer in the view. I believe that the offset should be computed as:

// center the hypnosis view in the scrollview

CGPoint offset;
offset.x = (reallyBigRect.size.width - wholeWindow.size.width)*.5;
offset.y = (reallyBigRect.size.height - wholeWindoe.size.height)*.5;
[scrollView setContentOffset:offset];

essentially centering the scroll view inside the bigger rectangle. It is a happy coincidence that the code in the book works for scroll views half the size of the external view.



Yes, you are correct. Given an arbitrary contentSize, you will have to compute the delta between the center of the content view and center of the scroll view to determine the appropriate offset.