Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView ScrollRectToVisible - not working with animate = yes

I have a UIScrollView which contains a button. When the button is pressed, I would like to scroll to the bottom of the view using scrollRectToVisible.

eg:

CGRect r = CGRectMake(0, myUIScrollView.contentSize.height - 1, 1, 1);
[myUIScrollView scrollRectToVisible:r animated:YES];

If I set animated to NO, everything works as expected, but if I set it to YES, I see really weird behaviour:

  • basically, nothing happens.
  • if I tap the button repeatedly, it may scroll a couple pixels, or may scroll all the way.
  • but if I scroll the view manually with a finger before pressing the button, it has a chance of scrolling to the bottom as expected, but it's not a sure thing.

I've printed _geScroll_Settings.contentSize, and it's as-expected.

I've also tried to delay the call to scrollRectToVisible by starting a timer, but the results are pretty much the same.

The scrollView is fairly vanilla. I'm creating it in interface builder. I am dynamically adding the scrollView's content at startup, and adjusting it's contentSize appropriately, but all that seems to be working fine.

Any thoughts?

like image 617
orion elenzil Avatar asked Aug 23 '11 23:08

orion elenzil


1 Answers

My bet is that scrollRectToVisible is crapping out because the visible area is not valid (1x1), or the y offset is just outside the bounds, have you tried setting it with the size of the visible area of the scrollView instead?

CGRect rectBottom = CGRectZero;
rectBottom.size = myUIScrollView.frame.size;
rectBottom.origin.y = myUIScrollView.contentSize.height - rectBottom.size.height;
rectBottom.origin.x = 0;

[myUIScrollView scrollRectToVisible:rectBottom animated:YES];

Sorry I can't help you out more, but I'm not on my Mac right now, so I can't run a test. The code above would create a CGRect of the exact size of what fits inside the scrollView visible portion, and the offset would be the last visible portion in it.

like image 122
Can Avatar answered Oct 28 '22 07:10

Can