我有一个静态 tableView
,每个 cell
中有一个 textfield
。单元格高度为50。我有以下方法来处理键盘:
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}
现在,当我输入 textField
时,如果它位于键盘下方,它会自动滚动到 textField
。实际上,我不确定这是怎么发生的,因为我没有在任何地方配置这种行为。这里发生了什么?
我真正想要的是滚动到 cell
的底部,而不是 textField
。目前,它滚动到 textField
但它切断了 cell
的一部分并且看起来不太好。
contentInset.bottom
是正确的,因为当键盘存在时,我可以完美地手动滚动到最后一个单元格。
我可能遗漏了一些 native 行为。
最佳答案
我会这样做(未测试)。在主视图的坐标系中找出单元格的框架。如果单元格底部低于键盘顶部,则调整表格 View 的内容偏移量...
UITableViewCell *cell = // get the cell containing the text field
CGRect bounds = [self.tableView convertRect:cell.frame toView:self.view];
CGFloat cellBottom = bounds.origin.y + bounds.size.height;
if (cellBottom > kbSize.height) {
[self.tableView setContentOffset:(cellBottom-kbSize.height) animated:YES];
}
有几个 SO 答案可以帮助您从文本字段中获取单元格。最好的方法是沿着 super View 指针向上移动,直到找到 UITableViewCell。 Here's an example where I answered this for a collection view
关于ios - 如何滚动到 UITableViewCell 的底部而不是 TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25556418/