Search code examples
iosobjective-ciphoneuiscrollviewuiscrollviewdelegate

Check if a UIScrollView reached the top or bottom


Is there a way to know if a UIScrollView has reached the top or bottom? Possibly in the method:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView 
                  willDecelerate:(BOOL)decelerate

Solution

  • Implement the UIScrollViewDelegate in your class, and then add this:

    -(void)scrollViewDidScroll: (UIScrollView*)scrollView
    {
        float scrollViewHeight = scrollView.frame.size.height;
        float scrollContentSizeHeight = scrollView.contentSize.height;
        float scrollOffset = scrollView.contentOffset.y;
    
        if (scrollOffset == 0)
        {
            // then we are at the top
        }
        else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
        {
            // then we are at the end
        }
    }
    

    Hope this is what you are after! Else have a tinker by adding more conditions to the above code and NSLog the value of scrollOffset.