3

I am trying to load the contents of a file to a TextView on viewDidLoad() of a ViewController. I want the user to see the last content loaded from the file, so I tried the solutions mentioned here.

let bottom = textView.contentSize.height - textView.bounds.size.height
textView.setContentOffset(CGPoint(x: 0, y: bottom), animated:true)

The textview will scroll down but creates a large white space if we scroll down again.

sonnet
  • 567
  • 4
  • 6
  • is your controller embedded to tabViewcontroller ? – KKRocks Jun 12 '17 at 05:25
  • No, its a regular ViewController pushed to navigation stack – sonnet Jun 12 '17 at 05:29
  • what happens if you remove this line **(let bottom = textView.contentSize.height - textView.bounds.size.height textView.setContentOffset(CGPoint(x: 0, y: bottom), animated: true) )** can you share screenshot or elaboration ? – KKRocks Jun 12 '17 at 05:34
  • @KKRocks If I remove the line then the scroll will remain on top of the content and the user will have to scroll down for the last line in the textView – sonnet Jun 12 '17 at 05:51
  • then try this solution : textView.textContainerInset = UIEdgeInsetsZero textView.textContainer.lineFragmentPadding = 0 – KKRocks Jun 12 '17 at 05:52

2 Answers2

0

By updating the content offset you are actually adding that much offset to the content of scrollview. Here with

let bottom = textView.contentSize.height - textView.bounds.size.height textView.setContentOffset(CGPoint(x: 0, y: bottom), animated: true)

these you are adding extra space at the bottom. So for automatically scrolling the content to the last line try using scrollRangeToVisible function. Like in the top rated answer you have mentioned in your question that you have used to scroll the textview to the last line.

Aravind A R
  • 2,674
  • 1
  • 15
  • 25
  • I did tried this (scrollRangeToVisible) solution before this one and it was working, But the problem I found with this was that the scrolling appears choppy. Even though with setContentOffset() I am getting the scroll right, but this (question) issue persist – sonnet Jun 12 '17 at 07:06
  • So when you were using scrollRangeToVisible was it creating blank spaces in the bottom like how you are getting right now ? – Aravind A R Jun 12 '17 at 07:26
0

This minor change, resolved my issue:

let bottom = CGPoint(x: 0, y: textView.contentSize.height - textView.frame.size.height)
textView.setContentOffset(bottom, animated: false)

Posting my answer, so that someone else find this helpful. Thanku all for the support.

sonnet
  • 567
  • 4
  • 6