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.

5
  • is your controller embedded to tabViewcontroller ? Commented Jun 12, 2017 at 5:25
  • No, its a regular ViewController pushed to navigation stack Commented Jun 12, 2017 at 5: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 ? Commented Jun 12, 2017 at 5: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 Commented Jun 12, 2017 at 5:51
  • then try this solution : textView.textContainerInset = UIEdgeInsetsZero textView.textContainer.lineFragmentPadding = 0 Commented Jun 12, 2017 at 5:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
So when you were using scrollRangeToVisible was it creating blank spaces in the bottom like how you are getting right now ?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.