Detect UITextView scroll location
Asked Answered
R

3

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.

Retrograde answered 20/12, 2012 at 10:23 Comment(0)
C
23

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");
    }
}
Cordeliacordelie answered 20/12, 2012 at 10:30 Comment(2)
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?Retrograde
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).Cordeliacordelie
T
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" )

    }
}
Terle answered 9/2, 2017 at 4:7 Comment(0)
L
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
    }
}
Lali answered 20/12, 2012 at 10:31 Comment(1)
Good approach. When working with float-values though, I would add an error margin.Aruwimi

© 2022 - 2024 — McMap. All rights reserved.