12

I cant seem to find a right solution to autoscroll UITextView using Swift.

For my app I am receiving constant data from BT, and I am updating it on a UITextView. However, when the UITextView will reach the latest updated line, I would have to manually scroll.

What I'm trying to achieve is to keep the last added line to the UITextView visible. (using swift)

1
  • I'm not at my computer right now so all I can do is you could try using a timer and increase the contentOffset. This will make it auto scroll. And when you reached the end, you can invalidate the timer to stop the scroll. Commented Jan 26, 2016 at 3:59

3 Answers 3

18

This will scroll to the bottom of a UITextView, with an animation:

let range = NSMakeRange(textView.text.characters.count - 1, 0)
textView.scrollRangeToVisible(range)
Sign up to request clarification or add additional context in comments.

Comments

5

Swift 5 update for @SwiftArchitect answer:

let range = NSRange(location: textView.text.count - 1, length: 0)
textView.scrollRangeToVisible(range)

Comments

1

You could also set contentOffset:

let point = CGPoint(x: 0.0, y: (textView.contentSize.height - textView.bounds.height))
textView.setContentOffset(point, animated: true)

It will scroll to the bottom.

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.