1

I am using a UITextView to display a long text in ViewDidLoad():

var story1 = "Researchers on Friday introduced the stunningly realistic-looking interactive humanoid robot to the world, and she reacted by nodding her head, squinting, moving her hand and eyeballs and telling assembled local photographers, with her speech synched to her lip movements, to snap her face from a more flattering angle. Girl's got spirit. \n\nJia Jia made her public debut at the University of Science and Technology of China, where she came to life over the course of three years. While her rosy cheeks and pink lips make her look like she has blood running through her actuators, she still has a way to go before she'll be confused with a human. Her expressions are currently limited to 'microexpressions' and her speech has that distinct stiffness that screams 'I am a robot and I will one day chase you from your home'\n\......." // a very long text with many rows
self.storyTextView.text = story1

If I set Scrolling Enabled for storyTextView, the textview displays the text not from the beginning. I can see the scroll indicator in the middle (or bottom) of the text.

see screenshot1. textview does not display text from the begining

I tried

self.storyTextView.scrollsToTop = true

but it does not help.

If I uncheck Scrolling Enabled, or if the text is not too long to display whole text in a UITextView, it displays from the beginning as expected. See screenshot2.

textview display from the begining

Is there any way to display the long text from the beginning of the text (move scroll indicator to the top)? I checked constraints and layout but it seems not related. I did search a lot but no luck until now. I might missed some simple setting but anyone can help me out please.

5 Answers 5

7

Use contentOffset property:

textView.contentOffset = CGPointZero

Update for Swift 3:

textView.contentOffset = CGPoint.zero
Sign up to request clarification or add additional context in comments.

3 Comments

thank you. you are correct. And I know from another question's answer that this need to be done in viewDidLayoutSubviews(). stackoverflow.com/questions/28053140/…
I add this and the issue solved. override func viewDidLayoutSubviews() { self.StoryTextView.setContentOffset(CGPointZero, animated: false) }
Swift 3: CGPointZero -> CGPoint.zero
2

The only workaround which is working on iOS 11 and swift 4 is to set scroll off at viewDidLoad and set ON at viewDidAppear. It works for both UIScrollView and UiTextView. Tested code.

 override func viewDidLoad() {
    super.viewDidLoad()
    scrollView.isScrollEnabled = false
 }

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    scrollView.isScrollEnabled = true
 }

Comments

1

Try this: self.storyTextView.scrollRangeToVisible(NSRange(location:0, length:0))

1 Comment

What makes you think he is not doing it on main thread already?
0

In my case this solution is work, Swift 4:

override func viewDidLayoutSubviews() {
    self.txt_about.setContentOffset(CGPoint.zero, animated: false)
}

Comments

0

Add the below method in ViewController

 override public func viewWillLayoutSubviews() {
   super.viewWillLayoutSubviews()
   textView.contentOffset = CGPoint.zero
 }

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.