UITextView从文本的底部或中间开始 [英] UITextView starts at Bottom or Middle of the text
问题描述
我会正确的。我在我的视图中放置了一个 UItextView
,当需要滚动查看所有文本时(当textView中存在大量文本时)textView在中间开始文本有时和文本的其他时间的底部。
I'll get right to it. I have a UItextView
placed in my view that when needs to scroll to see all the text (when a lot of text is present in the textView) the textView starts in the middle of the text sometimes and the bottom of the text other times.
在textView上未启用编辑。我需要一种方法来强制textView每次都从顶部开始。我看到一些问题,其他人使用了内容偏移,但我真的不知道它是如何工作的,或者它是否适用于此处。
Editing is not enabled on the textView. I need a way to force the textView to start at the top, every time. I saw some questions somewhat like this where other people used a content offset, but I do not really know how that works or if it would even be applicable here.
感谢您的帮助。
推荐答案
这就是诀窍我!
[self.textView scrollRangeToVisible:NSMakeRange(0, 0)];
Swift:
Swift:
self.textView.scrollRangeToVisible(NSMakeRange(0, 0))
Swift 2(替代解决方案)
将此覆盖方法添加到ViewController
Swift 2 (Alternate Solution)
Add this override method to your ViewController
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
textView.setContentOffset(CGPointZero, animated: false)
}
Swift 3& 4(语法编辑)
Swift 3 & 4 (syntax edit)
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
textView.setContentOffset(CGPoint.zero, animated: false)
}
这篇关于UITextView从文本的底部或中间开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!