UIScrollView, reaching the bottom of the scroll view

£可爱£侵袭症+ 提交于 2019-11-28 03:34:23

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.

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
    }
}

I Think @bensnider answer is correct, But not exart. Because of these two reasons

1. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{}

This method will call continuously if we check for if (bottomEdge >= scrollView.contentSize.height)

2 . In this if we go for == check also this condition will valid for two times.

  • (i) when we will scroll up when the end of the scroll view touches the bottom edge
  • (ii) When the scrollview bounces back to retain it's own position

I feel this is more accurate.

Very few cases this codition is valid for two times also. But User will not come across this.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
 {
    if (scrollView.contentOffset.y == roundf(scrollView.contentSize.height-scrollView.frame.size.height)) {
    NSLog(@"we are at the endddd");

   //Call your function here...

    }
}

The accepted answer works only if the bottom contentInset value is non-negative. A slight evolution would consider the bottom of the contentInset regardless of it's sign:

CGFloat bottomInset = scrollView.contentInset.bottom;
CGFloat bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height - bottomInset;
if (bottomEdge == scrollView.contentSize.height) {
    // Scroll view is scrolled to bottom
}

Actually, rather than just putting @bensnider's code in scrollViewDidScroll, this code (written in Swift 3) would be better performance-wise:

func scrollViewDidEndDragging(_ scrollView: UIScrollView,
                              willDecelerate decelerate: Bool) {
    if !decelerate {
        checkHasScrolledToBottom()
    }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    checkHasScrolledToBottom()
}

func checkHasScrolledToBottom() {
    let bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height
    if bottomEdge >= scrollView.contentSize.height {
        // we are at the end
    }
}

It work in Swift 3:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y == (scrollView.contentSize.height - scrollView.frame.size.height) {
        loadMore()
    }
}

See what items are currently displayed in the UIView, using something like indexPathsForVisibleRows and if your model has more items than displayed, put an arrow at the bottom.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!