我正在使用firebase在我的应用程序中实现实时聊天,如果显示键盘并且用户连续发送了10条消息,他的消息最终会隐藏在我的键盘后面或者从tableview视图下面出去(如果键盘没有显示)。我认为这与调整视图以适应tableview中的行数有关。最终,每次发送消息或接收最新消息时,我都需要将最新的单元格放在文本字段的正上方(无论是否显示键盘)。有小费吗?我看了github上的firechat,他们似乎没有任何代码:https://github.com/firebase/firechat-ios/blob/master/Firechat/ViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)notification
{
[self moveView:[notification userInfo] up:YES];
}
- (void)keyboardWillHide:(NSNotification*)notification
{
[self moveView:[notification userInfo] up:NO];
}
- (void)moveView:(NSDictionary*)userInfo up:(BOOL)up
{
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]
getValue:&keyboardEndFrame];
UIViewAnimationCurve animationCurve;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]
getValue:&animationCurve];
NSTimeInterval animationDuration;
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]
getValue:&animationDuration];
// Get the correct keyboard size to we slide the right amount.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
int y = keyboardFrame.size.height * (up ? -1 : 1);
self.view.frame = CGRectOffset(self.view.frame, 0, y);
[UIView commitAnimations];
}