禁用 UITextView 滚动包含视图?
我在 UIScrollView 中有一个 UITextView (具体来说,文本视图位于表中的 UITableViewCell 中)。当我在文本视图中键入并按回车键以创建一些新行时(并且文本视图中的文本变得太长而无法垂直包含),它会滚动文本视图本身(这是可以的)和包含它的表/表格单元格(我想阻止)。有什么方法可以防止这种行为吗?
- UITableView 将scrollEnabled 设置为NO,但UITextView 仍然导致其滚动。
- 我曾考虑过对 UITextView 进行子类化并覆盖 scrollRectToVisible 不执行任何操作,但我不介意文本视图本身滚动,我想阻止它滚动包含视图。
- 同样,在文本视图上将scrollEnabled设置为NO只会阻止其滚动,而不会阻止表/表单元格的滚动。
- 文本视图并没有被键盘遮挡,但看起来它仍然想向屏幕顶部靠拢。
I've got a UITextView inside a UIScrollView (specifically, the text view is in a UITableViewCell in a table). When I type in the text view and hit return to make some new lines (and the text in the text view becomes too long for it to contain vertically), it scrolls both the text view itself (which is okay) and the table/table cell that contains it (which I would like to prevent). Is there some way to prevent this behaviour?
- The UITableView has scrollEnabled set to NO, yet the UITextView still causes it to scroll.
- I've thought about subclassing UITextView and overriding scrollRectToVisible to do nothing, but I don't mind the text view itself scrolling, I want to stop it scrolling the containing view.
- Similarly, setting scrollEnabled to NO on the text view only prevents it from scrolling, not the table/table cell.
- The text view is not obscured by the keyboard, but it seems like it still wants to move closer to the top of the screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个问题已经快一年了,但它让我抓狂了一段时间。
我最终设法通过子类化
UITextView
来禁用该行为,重写名为scrollRectToVisibleInContainingScrollView
的方法,并且在实现中完全不执行任何操作。它是 UITextView 的一个私有的、未记录的方法,显然处理超级视图的自动滚动。由于我们所做的只是子类化和重新定义一个方法(因此不依赖它预先存在的),所以它应该是可以接受的。
This question is almost a year old, but it drove me crazy for a while.
I finally managed to disable that behavior by subclassing
UITextView
, overriding a method namedscrollRectToVisibleInContainingScrollView
and do absolutely nothing in the implementation.It is a private, undocumented method of UITextView that apparently handles the auto-scrolling of a super view. Since all we're doing is subclassing and redefining a method (and thus not relying on it existing beforehand), it should be acceptable.
不管怎样,我最终解决了这个问题,方法是在编辑时从 UITableViewCell 中删除 UITextView(并将其添加到包含表视图的视图控制器中),然后在完成后将其返回到 UITableViewCell。
For what it's worth, I've finally resolved this issue by removing the UITextView from the UITableViewCell (and adding it to the view controller that contains the table view) while editing, then returning it to the UITableViewCell when finished.
目前尚不清楚您到底希望它如何工作。如果您只需要防止自动滚动,那么这个答案可能会有所帮助。当 UITextView 的高度增加时,如何获得与标准提醒应用程序类似的行为?我已经在我的 TableKit 库 的示例中实现了这种行为
It is not clear how exactly you want it to work. In case you just need to prevent autoscrolling then this answer might help. How about to get similar behavior as in standard reminder app, when the height of
UITextView
increases? I have implemented such behaviour in samples for my TableKit library我认为乔纳森·M 有正确的解决方案(我对他投了赞成票)。
我在 UITableViewCell 中有 UITextView,当用户键入文本并且文本超出滚动视图的内容矩形时,它偶尔会向上滚动表视图。
我没有使用子类,而是使用了类别。
I think Jonathan M has the right solution (I upvoted him).
I had UITextView in a UITableViewCell that would occasionally scroll the table view up when the user was typing text and the text went beyond the content rect of the scroll view.
Instead of subclass, I used a category.