在我的代码中,每次弹出键盘时,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/