Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView, reaching the bottom of the scroll view

I know the Apple documentation has the following delegate method:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // called when scroll view grinds to a halt 

However, it doesn't necessarily mean you are at the bottom. Cause if you use your finger, scroll a bit, then it decelerates, but you are not actually at the bottom of your scroll view, then it still gets called. I basically want an arrow to show that there is more data in my scroll view, and then disappear when you are at the bottom (like when it bounces). Thanks.

like image 917
Crystal Avatar asked Jun 02 '11 17:06

Crystal


2 Answers

I think what you might be able to do is to check that your contentOffset point is at the bottom of contentSize. So you could probably do something like:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {     float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;     if (bottomEdge >= scrollView.contentSize.height) {         // we are at the end     } } 

You'll likely also need a negative case there to show your indicator when the user scrolls back up. You might also want to add some padding to that so, for example, you could hide the indicator when the user is near the bottom, but not exactly at the bottom.

like image 174
bensnider Avatar answered Oct 09 '22 18:10

bensnider


So if you want it in swift, here you go:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {      if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {         //reach bottom     }      if (scrollView.contentOffset.y < 0){         //reach top     }      if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y < (scrollView.contentSize.height - scrollView.frame.size.height)){         //not top and not bottom     } } 
like image 24
ytbryan Avatar answered Oct 09 '22 17:10

ytbryan