当新行添加到 UITextView 实例中时,如何向上滚动视图?
我已经成功设置了一个滚动视图,可以将一些文本字段和文本视图向上移动,以防它们被键盘隐藏。但是,当文本视图收到一些超出第一行的文本时,下一行显然会被键盘隐藏。因此,我正在寻找一种技巧,以便每次在 UITextview 实例中添加/删除新行时向上/向下移动视图。
感谢您的帮助,
斯蒂芬
I've successfully setup a scroll view that move some text fields and a text view up in case they are hidden by keyboard. However, when the text view received some text that goes beyond the first line the next lines are obviously hidden by keyboard. So, I'm looking for a trick to move up/down the view each time a new line is added/removed into/from the UITextview instance.
Thx for helping,
Stephane
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以
在 UITextView 委托上实现该方法,计算 textviews 文本中的换行符数量,计算必须滚动的距离,然后滚动。 (每次用户键入字符时都会调用此方法。)
或者,您可以采用更简单的路线并滚动到足够远以显示整个
UITextView
。如果用户输入的内容超出了文本视图的范围,文本视图将自动开始滚动。You can implement the
method on your UITextView delegate, count the number of newlines in the textviews text, calculate how far you have to scroll, and scroll. (This method is called every time the user types a character.)
Alternatively you can go the easier route and scroll far enough to display your entire
UITextView
. If the user types more than fits into the textview, the textview will start scrolling automatically.使用
scrollRectToVisible:animated:
或setContentOffset:animated:
- 在您的情况下,使用第一个,如下所示:您可能还需要将文本字段内容滚动到可见,但我认为这应该自动发生。
Use
scrollRectToVisible:animated:
orsetContentOffset:animated:
- in your case, the first, as in:You may also need to scroll your text field contents to visible, but I think that should happen automatically.
因此,如果您编译以前的答案:
只需将您的
textView.delegate
设置为 self 并将您的viewController
设置为
并且:So if you compile previous answers :
Just set your
textView.delegate
as self and yourviewController
as<UITextViewDelegate>
and :