如何实现[iOS]textView自动滚动至底部功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计235个文字,预计阅读时间需要1分钟。
在使用 `UITextView` 时,如果发现文本内容没有从顶部开始显示,可以尝试以下方法:
在 `viewWillAppear` 方法中重写代码,确保文本视图正确设置:
swiftoverride func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // 确保文本视图内容从顶部开始显示 textView.textContainerInset=UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)}
UITextView text content doesn't start from the top
参考:stackoverflow.com/questions/26835944/uitextview-text-content-doesnt-start-from-the-top
我使用的是这种方法:
yourTextView.isScrollEnabled = false
yourTextView.text = "....."
}
override func viewDidAppear(_ animated: Bool) {
yourTextView.isScrollEnabled = true
}
原理:当插入大量文本,会导致页面scroll向下,所以在viewWillAppear阶段先锁定页面的scroll,然后导入文本,使其不自动向下scroll。然后在viewDidAppear阶段,再允许scroll
本文共计235个文字,预计阅读时间需要1分钟。
在使用 `UITextView` 时,如果发现文本内容没有从顶部开始显示,可以尝试以下方法:
在 `viewWillAppear` 方法中重写代码,确保文本视图正确设置:
swiftoverride func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // 确保文本视图内容从顶部开始显示 textView.textContainerInset=UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)}
UITextView text content doesn't start from the top
参考:stackoverflow.com/questions/26835944/uitextview-text-content-doesnt-start-from-the-top
我使用的是这种方法:
yourTextView.isScrollEnabled = false
yourTextView.text = "....."
}
override func viewDidAppear(_ animated: Bool) {
yourTextView.isScrollEnabled = true
}
原理:当插入大量文本,会导致页面scroll向下,所以在viewWillAppear阶段先锁定页面的scroll,然后导入文本,使其不自动向下scroll。然后在viewDidAppear阶段,再允许scroll

![如何实现[iOS]textView自动滚动至底部功能?](/imgrand/aydZKsrQ.webp)