I have used UIScrollView
before, and am using it now, and never had a problem. I'm now adding it to an old app, and while it works as expected (I can look at the contents, scroll around with my finger, all the bounds and sizes are setup right so there is no empty space in the content, etc.), I just can't get scrollToRectVisible
to work. I have even simplified the call so that it merely moves to the 0,0 position:
[scrollView scrollRectToVisible:CGRectMake(0, 0, 10, 10) animated:YES];
or move it to 0,200:
[scrollView scrollRectToVisible:CGRectMake(0, 200, 10, 10) animated:YES];
I even made a quick app to test this and I can get scrollRectToVisible
to work there as I expect. But in my old app, I can't seem to make it do anything.
I can make the scrollView scroll with setContentOffset:
, but that's not what I want.
This scrollView and its contents are defined in the nib by IB and used with an IBOutlet. The only code I am using in my app to handle the scrollView is
[scrollView setContentSize:CGSizeMake(scrollView.contentSize.width, imageView.frame.size.height)];
(I'm only interested in vertical scrolling not horizontal).
Has anyone run into a problem like this?
I have compared the scrollView attributes in both apps and they are identical.
ADDENDUM:
My scrollViews frame is: 0.000000 0.000000 480.000000 179.000000
My scrollViews contentSize is: 0.000000 324.000000
It still acts like the rect I want to scroll to is already visible and no scrolling is needed. Not sure if that is what is happening. This is just the darnest thing. Seems like such an easy thing to resolve...
ADDENDUM #2:
This is how I am making do without scrollRectToVisible
:
CGPoint point = myRect.origin;
if (![clefScrollView pointInside:point withEvent:nil]) {
point.x = 0;
if (point.y > clefScrollView.contentSize.height - clefScrollView.bounds.size.height)
point.y = clefScrollView.contentSize.height - clefScrollView.bounds.size.height;
[clefScrollView setContentOffset:point animated: YES];
}
Everything else about this scrollView works as expected, but scrollRectToVisible
. WHY?!? Any wild guesses?
I had the same symptoms as this issue..the scrollRectToVisible was not working as intended. My problem was that the scrollview on the storyboard had a ambiguous constraint problem. Once that was fixed the call worked just fine. It is something else to check when the scrollRectToVisible call isn't working as intended.
The suggested solution above may still give an error if you haven't set the frame for the "clefScrollView".
If the solution above is used and still having the same problem, make sure you have initialized your scollView (in this case clefScrollView) frame prior to setting the content view.