我设法找出了如何做到这一点!
有 3 个非常重要的细节:
- 在不关闭键盘的情况下调整单元格大小的方式。 (正常重新加载会隐藏键盘)。
- 只有在内容偏移动画完成后才能设置单元格高度。 (否则 setContentOffset 可能会被忽略)。
- 为 tableView 设置底部插图。 (否则调整单元格大小可能会向下滚动表格,隐藏我们希望在键盘上方可见的单元格。)
此方案已在 iOS10 和 iOS11 中使用真 iPhone 进行测试。
下面是方法(所有代码都在 viewController 中实现):
- (TableViewScrollDirection) scrollToKeepEditingCellVisibleAboveVerticalPoint:(CGFloat)verticalPoint cellIndexPath:(NSIndexPath*)cellIndexPath anchorsToVerticalPoint:(BOOL)anchorsToVerticalPoint cellHeight:(CGFloat)cellHeight adjustsCellSize:(BOOL)adjustsCellSize {
// Remark: verticalPoint is the desired offset above the tableView bottom. In my case the height of the keyboard covering the bottom of the tableView
CGRect cellFrame = CGRectOffset([self.tableView rectForRowAtIndexPath:cellIndexPath], -self.tableView.contentOffset.x, -self.tableView.contentOffset.y - self.tableView.contentInset.top);
CGFloat cellBottom = adjustsCellSize ? cellFrame.origin.y + cellHeight : cellFrame.origin.y + cellFrame.size.height;
CGFloat offsetNeeded = cellBottom - verticalPoint; // Relative offset
CGFloat brandNewOffset = self.tableView.contentOffset.y + offsetNeeded; // Absolute offset
if ((offsetNeeded > 0) || ((offsetNeeded < 0) && anchorsToVerticalPoint))
{
CGFloat elasticity = self.tableView.frame.size.height - verticalPoint;
if (self.tableView.contentInset.bottom != elasticity)
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, elasticity, 0); // This will make sure the tableview does not scroll down when its content offset elasticity is not enough
if (adjustsCellSize)
[self setContentOffsetAndAdjustCellSizes:brandNewOffset];
else
[self.tableView setContentOffset:CGPointMake(0, brandNewOffset) animated:YES];
if (offsetNeeded > 0)
return TableViewScrollUp;
else if (offsetNeeded < 0)
return TableViewScrollDown;
}
return TableViewScrollNone;}
技巧的第二部分在于仅在滚动动画结束后调整单元格大小:
- (void) setContentOffsetAndAdjustCellSizes:(CGFloat)contentOffset{
[UIView animateWithDuration:0.5 animations:^
{
[self.tableView setContentOffset:CGPointMake(0, contentOffset) animated:NO];
}
completion:^(BOOL finished)
{
[self.tableView beginUpdates]; // Cell height must be adjusted this way, otherwise the keyboard closes.
[self.tableView endUpdates];
}];}
很重要:键盘关闭后,平滑重新调整tableview滚动(如有必要):
- (void) keyboardDidHide:(NSNotification *)notification {
if (self.tableView.contentInset.bottom != 0)
[UIView animateWithDuration:0.5 animations:^ {self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);}];
self.activeKeyboardSize = CGSizeZero; }
一切如何开始:
- (void) keyboardDidShow:(NSNotification*)notification {
NSDictionary* info = [notification userInfo];
self.activeKeyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGFloat tableViewBottom = self.tableView.frame.origin.y + self.tableView.frame.size.height;
CGFloat keyboardTop = self.view.frame.size.height - self.activeKeyboardSize.height;
CGFloat coveringVerticalSpace = tableViewBottom - keyboardTop;
if (coveringVerticalSpace <= 0)
return;
TableViewScrollDirection scrollDirection = [self scrollToKeepEditingCellVisibleAboveVerticalPoint:self.tableView.frame.size.height - coveringVerticalSpace - UI_MARGIN_DEFAULT anchorsToVerticalPoint:NO];
if (scrollDirection == TableViewScrollUp)
self.textControlCellHadToMoveUpToBeVisibleOverKeyboard = YES;}
用户也可以直接从一个单元格中的一个 textField 或 textView 跳转到另一个单元格中的另一个,而无需关闭键盘。必须考虑和处理这一点。
每当 textView 文本发生变化时,也应该调用滚动方法,因为它的大小和单元格的大小也需要改变:
CGFloat tableViewBottom = self.tableView.frame.origin.y + self.tableView.frame.size.height;
CGFloat keyboardTop = self.view.frame.size.height - self.activeKeyboardSize.height;
CGFloat coveringVerticalSpace = tableViewBottom - keyboardTop;
if (coveringVerticalSpace <= 0)
return;
[self scrollToKeepEditingCellVisibleAboveVerticalPoint:self.tableView.frame.size.height - coveringVerticalSpace - UI_MARGIN_DEFAULT anchorsToVerticalPoint:self.textControlCellHadToMoveUpToBeVisibleOverKeyboard cellHeight:staticCellView.frame.size.height adjustsCellSize:adjustsCellSize]; // UI_MARGIN_DEFAULT is 8.0 and it gives a little margin of 8 points over the keyboard.
也很有用:
typedef NS_ENUM(NSUInteger, TableViewScrollDirection){
TableViewScrollNone,
TableViewScrollDown,
TableViewScrollUp };
在 viewController 中创建一个有用的属性:
@property (nonatomic) BOOL textControlCellHadToMoveUpToBeVisibleOverKeyboard;
我希望这会有用。