14

I am trying to implement a form of a Terms & Conditions page where the "Proceed" button is only enabled once the user has scrolled to the bottom of a UITextView. So far I have set my class as a UIScrollView delegate & have implemented the method below:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"Checking if at bottom of UITextView");
    CGPoint bottomOffset = CGPointMake(0,self.warningTextView.frame.size.height);
    //if ([[self.warningTextView contentOffset] isEqualTO:bottomOffset])
    {
    }    
}

I have commented the if statement because I am not sure how to check if the UITextView is at the bottom.

Cœur
  • 37,241
  • 25
  • 195
  • 267
JamesLCQ
  • 341
  • 6
  • 12

3 Answers3

22

UITextView is a UIScrollView subclass. Therefore the UIScrollView delegate method you are using is also available when using UITextView.

Instead of using scrollViewDidEndDecelerating, you should use scrollViewDidScroll, as the scrollview may stop scrolling without deceleration.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        NSLog(@"at bottom");
    }
}
howanghk
  • 3,070
  • 2
  • 21
  • 34
  • Thanks Owen, I will try this out this evening & update if successful (or not)! James Question: you mention scrollView in your example - does it matter if I reference my UITextView.contentOffSet.y etc? – JamesLCQ Dec 20 '12 at 11:11
  • 2
    the `scrollView` you concerned is the variable name, you can call it anything you like as long as you change the one in the method name too. For example you can call it `textView`, and change the method name to `- (void)scrollViewDidScroll:(UIScrollView *)textView`. Or, you can reference your UITextView directly, and change the if line to `if (self.warningTextView.contentOffset.y >= self.warningTextView.contentSize.height - self.warningTextView.frame.size.height)`. – howanghk Dec 20 '12 at 11:36
8

A Swift version for this question:

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height {

        print( "View scrolled to the bottom" )

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Bright
  • 5,699
  • 2
  • 50
  • 72
0

This should solve it. It works. I am using it.

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
    float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (bottomEdge >= scrollView.contentSize.height) 
    {
        // we are at the end
    }
}
Akshay Shah
  • 1,120
  • 6
  • 13