UIScrollview bottom detection

I have a UIScrollView with images, and when the user scrolls to the end of the scrollview, I want to update the content. This is my code to determine when the bottom of the scroll is reached:

The code below is implemented in scrollViewDidScroll: delegate

 CGFloat scrollViewHeight = imagesScrollView.bounds.size.height; CGFloat scrollContentSizeHeight = imagesScrollView.contentSize.height; CGFloat bottomInset = imagesScrollView.contentInset.bottom; CGFloat scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight; if(imagesScrollView.contentOffset.y > scrollViewBottomOffset){ [imagesView addSubview:imagesBottomLoadingView]; [self downloadImages]; } 

My problem is that when the user scrolls down, my function is called several times, but I want to call it only once. I tried with imagesScrollView.contentOffset.y == scrollViewBottomOffset, but it does not work and the function is not called

+13
ios uiscrollview
source share
7 answers

You were thinking of adding a boolean. update it when the method is called for the first time and possibly when the user scrolls the backup.

+2
source share

If you want to quickly find them:

 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 }} 
+23
source share
 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height; if (bottomEdge >= scrollView.contentSize.height) { // we are at the end } } 
+4
source share

Carlos, the answer is better.

For Swift 4.x, you must change the method name:

 func scrollViewDidScroll(_ scrollView: UIScrollView) { if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) { //bottom reached } } 
+4
source share

implement scrollViewDidScroll: and check the contentOffset so that to reach the end

0
source share

Sometimes you have to use +1 in the condition because contentSize.height gives you a few decimal places, so if you use this, you avoid this ...

 override func scrollViewDidScroll(scrollView: UIScrollView) { if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) { //bottom reached } } 
0
source share

For Swift 4.5:

 func scrollViewDidScroll(_ scrollView: UIScrollView) { let scrollViewHeight = scrollView.frame.size.height let scrollContentSizeHeight = scrollView.contentSize.height let scrollOffset = scrollView.contentOffset.y if (scrollOffset == 0) { // then we are at the top print("then we are at the top") } else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight) { print("then we are at the end") // then we are at the end } } 
0
source share

All Articles