UITableViewCell 中 UITextField 的原始值
我想在显示键盘时向上滚动 UITableView
,这样文本字段就不会被键盘覆盖。我知道如何滚动 tableView 和 contentInset
等。我在计算滚动距离时遇到困难。
我有键盘尺寸(来自 UIKeyboardDidShowNotification
)。但是,我无法为正在编辑的文本字段获取有用的原始值。传递给 textFieldDidBeginEditing
的 UITextField
的来源没有帮助:无论正在编辑哪个 textField,该值都是相同的。 textField.superview.origin
也没有帮助。
如何获取正在编辑的文本字段的原点的有用值?
我将使用该值以及键盘高度来确定滚动距离。
I'd like to scroll my UITableView
up when the keyboard is displayed so the textfields are not covered by the keyboard. I know how to scroll the tableView and the contentInset
etc. I'm having trouble calculating the distance to scroll.
I have the keyboard dimensions (from UIKeyboardDidShowNotification
). However I'm having trouble obtaining a useful origin value for the textfield that's being edited. The origin of the UITextField
passed to textFieldDidBeginEditing
isn't helpful: the value is the same regardless of the textField that's being edited. textField.superview.origin
isn't helpful either.
How can I obtain a useful value for the origin of the textField that's being edited?
I'll use this value along with the keyboard height to determine the distance to scroll.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,我认为最好的是更改表视图的框架,以便它根本不会被键盘隐藏(您可以查看键盘框架并基于此更改表视图框架)。然后使用
scrollToRowAtIndexPath:atScrollPosition:animated:
滚动表格视图,使单元格可见。这样
UITableView
就会为您处理实际的内容偏移量。Personally what I think is best is to change the frame of the table view so that it's not hidden at all by the keyboard (you can look at the keyboard frame and change the table view frame based on that). Then scroll the table view using
scrollToRowAtIndexPath:atScrollPosition:animated:
so that the cell is visible.That way
UITableView
handles what the actual content offset should be for you.如果行高和标题高度相同,您可以简单地获取行值并将其乘以行高以获得正确的原点。如果您使用可变行高/节标题,那么这种方法会变得有点复杂。
第二种方法是在我打字时由 MattJGalloway 发布的,是更改 tableview 的框架,然后让 tableView 自行滚动。这也是我的首选方法。
If your row heights and header heights are all the same, you could simply obtain your row value and multiply this by your row height to obtain the correct origin. If you are using variable row heights/section headers, then it becomes a bit more complicated with this approach.
The second approach, just posted by MattJGalloway as I was typing, is to change the frame of the tableview and then let the tableView scroll itself. This would be my preferred approach as well.