views:

2515

answers:

4

HI. 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?

+1  A: 

Yeah, I have not had success with scrollRectToVisible:animated:, but setContentOffset:animated: always works for me. Out of curiosity, why do you not want to use setContentOffset:animated:? It seems to be the proper solution.

Jonathan Sterling
Unfortunately, setContentOffset will scroll to the point, regardless of whether it is already visible or not. I could do the extra work of determining if the point is visible or not, however, I'm still curious why scrollRectToVisible isn't working, when I see it work in my own sample and other samples on the net. I must be doing something wrong, but with only three or four lines of code for the scrollView, I can't see what?!
mahboudz
+5  A: 

Over a month later, and I finally figured it out. While the alternative above worked well, it has been bugging me and I had to finally figure it out. Turns out that it was somewhat of a stupid mistake.

Here's the old code in my viewDidLoad, where I set up the scrollView:

[clefScrollView setContentSize:CGSizeMake(clefScrollView.contentSize.width, clefView.frame.size.height)];

The value of a scrollView height or width can't be 0! I think this got past me because I was assuming that ScrollView sizes start out with a valid size, and I was missing the fact that one dimension was zero!

This works:

[clefScrollView setContentSize:CGSizeMake(clefView.frame.size.width, clefView.frame.size.height)];

Hope this helps someone out there. I definitely spent way too much time trying to debug this.

mahboudz
You most certainly DID help someone out. Been wrestling with this all night until I found this. THANKS!
Bdebeez
+1  A: 

nice detective work, mahboudz.

fwiw, i suspect that the problem isn't precisely that the ScrollView can't have a dimension of size zero, but rather that it must be sufficiently large to contain the requested rectangle !

cheers, Orion

orion elenzil
+1  A: 

mahboudz: Thanks for this! Exactly my problem.

Marc Rochkind