2

I have a textView , and it's text content will increase . I want to know how to set a bool value , like isTextViewScrollToBottom:Bool when textView is scroll to bottom, then isTextViewScrollToBottom = true when textView is leave bottom, then isTextViewScrollToBottom = false

now, I only know can use scrollViewDelegate func like:

scrollViewDidEndDecelerating(_ scrollView: UIScrollView),

scrollViewWillBeginDragging(_ scrollView: UIScrollView),

scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)

and I try this:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    checkIsScrollToBottom(scrollView)
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    checkIsScrollToBottom(scrollView)
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
   checkIsScrollToBottom(scrollView)
}

fileprivate func checkIsScrollToBottom(_ scrollView: UIScrollView){
    if textViewForChatContent.contentSize.height - scrollView.contentOffset.y == 301 {
        isScrollToBottom = true
        print("isScrollToBottom=\(isScrollToBottom)")
    }else{
        isScrollToBottom = false
        print("isScrollToBottom=\(isScrollToBottom)")
    }
}

but this Judgment is not good use for all device. is someone know better Judgment, can know textView is scroll to bottom?

thanks!

DanielTing
  • 35
  • 1
  • 7
  • The duplicate is in Objective-C , but since the solution is just a basic one line calculation of the scrollOffset, I marked it as a duplicate. –  Feb 09 '17 at 02:55
  • thank a lot , it solve my problem! – DanielTing Feb 09 '17 at 03:30
  • @Sneak maybe a swift version of this question would be more friendly to beginners, I used to freak out about OC code :) – Bright Feb 09 '17 at 04:00
  • @BrightFuture Go ahead and port it to Swift and answer here, DanielTing could accept it as the answer. I removed the duplicate tag. here is the Obj-C version : http://stackoverflow.com/questions/13969970/detect-uitextview-scroll-location , or update the existing question with a Swift version (?) –  Feb 09 '17 at 04:01
  • @Sneak there's a `Swift` tag on this question – Bright Feb 09 '17 at 04:04

1 Answers1

8

From this question

You can use this function:

func scrollViewDidScroll(_ scrollView: UIScrollView) {

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

        print( "View scrolled to the bottom" )

    }
}
Community
  • 1
  • 1
Bright
  • 5,699
  • 2
  • 50
  • 72