Detect UITextView scroll location

你说的曾经没有我的故事 提交于 2019-12-04 00:48:24

问题


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.


回答1:


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");
    }
}



回答2:


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" )

    }
}



回答3:


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


来源:https://stackoverflow.com/questions/13969970/detect-uitextview-scroll-location

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