ios keyBoardShow 与 UITableView

标签 ios uitableview keyboard

在我的代码中,每次弹出键盘时,tableView 都会向上推,所以我可以看到 tableView 的底部。但是前三四个单元格将隐藏在 View 后面。当键盘离开时,scrollView 的顶部似乎被切断了。

如何在发短信时让我的 tableview 完整显示?

这是我的代码...

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated]; 
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardShow:(NSNotification *)note{
  CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  CGFloat deltaY=keyBoardRect.size.height;

  [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
  self.view.transform=CGAffineTransformMakeTranslation(0, -deltaY);
  }];
}
-(void)keyboardHide:(NSNotification *)note{
  [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
    self.view.transform = CGAffineTransformIdentity;
  }];
}

向单元格添加文本后滚动到底部。

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(self.data.count-1) inSection:0];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

最佳答案

与其在键盘显示时变换 View ,不如通过调整其框架来缩小 View :

CGRect newFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height-deltaY);
self.view.frame = newFrame; 

关于ios keyBoardShow 与 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25781522/

相关文章:

iphone - 表格 View 中的图像显示在模拟器中,但未显示在我的 iPod 设备中

android - Apache Cordova InAppBrowser 显示键盘并立即隐藏它

ios - 新语言键盘 iOS

安卓虚拟键盘开启大写锁定

iphone - 使用 GDataXML 在 Objective C 中更改 xml 中的属性值

ios - 在快速平移滑动手势期间获取 y 坐标的最简单方法

ios - 隐藏 UIView 时屏幕自动调整大小

ios - 如何使单元格不与其他单元格一起滚动?

objective-c - MKMapView 与一个 MKOverlayPathView 会导致 map 模糊

ios - 如何使用 Swift 选择 iOS 照片应用程序中的所有 UICollectionView 单元格?