【发布时间】:2013-07-11 00:45:55
【问题描述】:
在我的视图中,我的 UITableViewCell 中有一个 UITextField。现在有可能 UITextField 可能在我的键盘后面,所以我使用以下两种方法正确处理。一个是键盘被点击的时候,一个是键盘被关闭的时候:
- (void)keyboardNotification:(NSNotification*)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CustomCell *cell = (CustomCell*)[[tempTextField superview] superview];
CGRect textFieldRect = [cell convertRect:tempTextField.frame toView:self.view];
if (textFieldRect.origin.y + textFieldRect.size.height >= [UIScreen mainScreen].bounds.size.height - keyboardSize.height) {
thetableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0);
NSIndexPath *pathOfTheCell = [thetableView indexPathForCell:cell];
[thetableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:pathOfTheCell.row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
- (void)keyboardhideNotification:(NSNotification*)notification {
thetableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
现在它工作得有些好,但是有两个问题。
- 只有当整个 UITextField 低于键盘顶部时,tableview 才会滚动到键盘上方。如果键盘位于被选中的 UITextField 的一半,则 tableview 将不会滚动到键盘上方。乍一看,这应该可以工作,但我可能遗漏了一些东西。
2 .当键盘位于键盘下方并且表格视图向上滚动时,它会以一种很好的动画方式进行,但是当我单击完成时,它会立即弹回旧位置。我可以清楚地看到为什么会发生这种情况,但是我将如何制作漂亮的动画以恢复到 tableview 所在的旧位置?
任何意见将不胜感激!
更新:我设法找到了 #1 的问题。这是一个愚蠢的错误。应该将 textField 的高度添加到原点,因为该测量值正在下降。现在进入#2...
【问题讨论】:
标签: ios uitableview keyboard scroll uitextfield