当键盘隐藏时,UITableView稍微上升

在此处输入图像描述

我正在使用UITableView(chatTable)和UITabBar(chatTabBar)以及imageView中的一个textField。 我正在使用autolayout。 当键盘出现和消失时,我使用以下代码更改视图。

- (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; // get animation info from userInfo NSTimeInterval animationDuration; CGRect keyboardFrame; [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame]; // resize the frame [UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.keyboardHeight.constant = keyboardFrame.size.height - TABBAR_HEIGHT ; [self.view layoutIfNeeded]; } completion:nil]; if ([chatData count] != VALUE_ZERO) { [chatTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([chatData count] - VALUE_ONE) inSection:VALUE_ZERO] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; } } - (void)keyboardWillHide:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; // get animation info from userInfo NSTimeInterval animationDuration; CGRect keyboardFrame; [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame]; // Set view frame [UIView animateWithDuration:animationDuration delay:2.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.keyboardHeight.constant -= keyboardFrame.size.height - TABBAR_HEIGHT; [self.view layoutIfNeeded]; } completion:nil]; } 

现在,当我按下返回时,tableview上升了一小部分(从屏幕2到屏幕3)。 keyboardHeight是tabBar和主视图之间的底部空间约束。

在此处输入图像描述 (屏幕2)


在此处输入图像描述 (SCREEN3)

我尝试了很多东西,但是我无法找到为什么tableview会持续一段时间。 (问题是没有平滑的动画。)(注意:我只把延迟设为2.0,以显示后面的截图(屏幕3)中发生的情况,否则它的值将为0)

您的问题是当键盘出现时您正在更改表格视图框架,这是错误的。 您需要更改表视图的contentInset属性,而不是使用框架进行干预。

 - (void)keyboardWillShow:(NSNotification *)notification { CGFloat height = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height - self.tabBarController.tabBar.frame.size.height; UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0.0f, 0.0f, height, 0.0f); _tableView.contentInset = edgeInsets; _tableView.scrollIndicatorInsets = edgeInsets; } - (void)keyboardWillHide:(NSNotification *)notification { UIEdgeInsets edgeInsets = UIEdgeInsetsZero; _tableView.contentInset = edgeInsets; _tableView.scrollIndicatorInsets = edgeInsets; } 

解决了contentInset属性的问题。 我正在使用@Eugene提到的contentInset,并且还会更改textfiled的bottom约束的constant属性,以便在显示和隐藏键盘时向上移动。