swift - UITextview 不滚动到顶部

标签 swift text scroll uitextview display

我正在使用 UITextView 在 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

如果我为 storyTextView 设置 Scrolling Enabled,则 TextView 不会从头开始显示文本。我可以在文本的中间(或底部)看到滚动指示器。

见屏幕截图 1。 textview does not display text from the begining

我试过了

self.storyTextView.scrollsToTop = true

但这并没有帮助。

如果我取消选中 Scrolling Enabled,或者如果文本不太长而无法在 UITextView 中显示整个文本,它会按预期从头开始显示。见截图 2。

textview display from the begining

有没有办法从文本的开头显示长文本(将滚动指示器移动到顶部)?我检查了约束和布局,但似乎不相关。我确实搜索了很多但直到现在都没有运气。我可能错过了一些简单的设置,但任何人都可以帮助我。

最佳答案

使用 contentOffset 属性:

textView.contentOffset = CGPointZero

Swift 3 更新:

textView.contentOffset = CGPoint.zero

关于swift - UITextview 不滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36721370/

相关文章:

css - 页面加载时滚动元素跳转

swift - Swift 闭包是否保留捕获的变量?

ios - Apple Health Kit 错误域=com.apple.healthkit 代码=5 “Authorization not determined”

ios - 使用导航 Controller 在滑出面板中显示右键

swift - 服务器端 Swift - 调度队列

Python - 将文本文件读入字典

python - 将字符串打印到文本文件

java - 使用 Java 将嵌套 Map 保存并加载到文本文件中

android - 如何解决滚动列表页面时不选择总 ListView ?

c# - 嵌套列表框滚动问题