我终于得到了我的方法,当键盘显示时,我的表格视图向上移动。
我的所有单元格都包含一个 textfield 和一些标签围绕它。到目前为止,原始方法(以及这个奇怪的......)只滚动到目前为止文本字段可见而不是整个单元格。
但为什么呢?我希望整个单元格向上滚动。我错误地计算了插图吗?我不这么认为......我的所有观点都在桌面视图下方有一个桌面视图和一个页脚视图(高度55)。有些人还在桌子上方有一个标题(高度80)。但是它们都没有将整个单元格滚动到视图中,只有文本区域 atScrollPosition参数的影响为零,它的滚动始终相同..
- (void)keyboardWasShown:(NSNotification*)aNotification
{
//All table views are embedded in the parent view
//The parent view y is defined by the status and navigation bar, the height by the tab bar
CGRect viewRect = self.view.frame;
CGRect tableRect = self.tableView.frame;
//The keyboard size will be adjusted that the height is really only the height overlapping the table
NSDictionary* info = [aNotification userInfo];
CGSize kbOriginalSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat effectiveKeyBoardHeight = kbOriginalSize.height - TABBARHEIGHT - (viewRect.size.height - tableRect.size.height - tableRect.origin.y); //the last origin property is important to find out if there is a header
if(effectiveKeyBoardHeight < 0) effectiveKeyBoardHeight = 0;
CGSize kbSize = CGSizeMake(kbOriginalSize.width, effectiveKeyBoardHeight);
//Now the content insets will be adjusted for the calculated part of the keyboard overlapping the table
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// I've changed the apple code here! They use viewRect..in my app this doesn't make any sense, tableRect is key
tableRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(tableRect, self.activeField.frame.origin) ) {
//[self.tableView scrollRectToVisible:cell.frame animated:YES];
[self.tableView scrollToRowAtIndexPath:[self indexPathForCellContainingUIElement:self.activeField] atScrollPosition:UITableViewScrollPositionNone animated:YES];
}
}