原来contentInset 和scrollIndicatorInsets 都不正确:(64, 0, **265**, 0)。底部插入值为 55 磅(即标签栏高度)。
正确的底部插图当然必须是键盘高度216。
在 Apple 齐心协力之前解决这个问题:
- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField
{
dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC));
dispatch_after(when, dispatch_get_main_queue(), ^(void)
{
// Must be delayed until after iOS has adjusted the insets,
// which turns out to be 'way before' the keyboard animation has finished.
[self.tableView setContentInset:UIEdgeInsetsMake(64, 0, 216, 0)];
[self.tableView setScrollIndicatorInsets:UIEdgeInsetsMake(64, 0, 216, 0)];
});
return YES;
}
和:
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
[textField resignFirstResponder];
// iOS seems to set the insets differential: So we must set them back
// to the incorrect values for iOS to subtract the bottom inset back
// to 55 again.
// No delay needed here of course because we must do this before the
// keyboard animation starts.
[self.tableView setContentInset:UIEdgeInsetsMake(64, 0, 265, 0)];
[self.tableView setScrollIndicatorInsets:UIEdgeInsetsMake(64, 0, 265, 0)];
return YES;
}
编辑:为确保此解决方法能够抵抗 Apple 修复,让我们这样做:
dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC));
dispatch_after(when, dispatch_get_main_queue(), ^(void)
{
if (self.tableView.contentInset.bottom == 265)
{
[self.tableView setContentInset:UIEdgeInsetsMake(64, 0, 216, 0)];
[self.tableView setScrollIndicatorInsets:UIEdgeInsetsMake(64, 0, 216, 0)];
}
});