iPhone - knowing if a UIScrollView reached the top or bottom

Duck picture Duck · Oct 9, 2011 · Viewed 59.4k times · Source

Is there a way to know if a UIScrollView has reached the top or bottom inside

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

?

thanks.

Answer

Luke picture Luke · Oct 9, 2011

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.