每次更改文本时,如何强制UITextView滚动到顶部? [英] How do I force a UITextView to scroll to the top every time I change the text?

查看:116
本文介绍了每次更改文本时,如何强制UITextView滚动到顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我在使用 UITextView 时遇到了一些问题。这是问题所在:

OK, I'm having some problem with the UITextView. Here's the issue:

我在 UITextView 中添加了一些文字。然后用户双击以选择某些内容。
我然后更改 UITextView 中的文本(以编程方式如上)和 UITextView 滚动到底部有光标的页面。

I add some text to a UITextView. The user then double clicks to select something. I then change the text in the UITextView (programatically as above) and the UITextView scrolls to the bottom of the page where there is a cursor.

但是,这不是用户点击的地方。无论用户点击何处,它总是滚动到 UITextView 的底部。

However, that is NOT where the user clicked. It ALWAYS scrolls to the bottom of the UITextView regardless of where the user clicked.

所以这是我的问题:每次更改文本时,如何强制 UITextView 滚动到顶部?我试过 contentOffset scrollRangeToVisible 。两者都没有。

So here's my question: How do I force the UITextView to scroll to the top every time I change the text? I've tried contentOffset and scrollRangeToVisible. Neither work.

任何建议都会受到赞赏。

Any suggestions would be appreciated.

推荐答案

UITextView*note;
[note setContentOffset:CGPointZero animated:YES];

这适合我。

Swift版本(在Xcode 9.3上使用iOS 11的Swift 4.1):

Swift version (Swift 4.1 with iOS 11 on Xcode 9.3):

note.setContentOffset(.zero, animated: true)

这篇关于每次更改文本时,如何强制UITextView滚动到顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆