Ios 当键盘隐藏时,UITableView会略微上升

Ios 当键盘隐藏时,UITableView会略微上升,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,我在imageView中使用UITableView(聊天表)、UITablebar(聊天栏)和一个文本字段。我正在使用自动布局。我使用以下代码在键盘出现和消失时更改视图 - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; // get animation in

我在imageView中使用UITableView(聊天表)、UITablebar(聊天栏)和一个文本字段。我正在使用自动布局。我使用以下代码在键盘出现和消失时更改视图

        - (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];
        }
现在,当我按下return键时,tableview会稍微上升一点(从屏幕2到屏幕3)。键盘高度是选项卡栏和主视图之间的底部空间约束

(屏幕2)


(屏幕3)


我尝试了很多方法,但是我找不到为什么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属性,而不是干预框架

- (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,并且还更改了文本字段底部约束的
常量
属性,以便在显示和隐藏键盘时向上移动和删除。

解决了contentInset属性的问题。我正在使用@Eugene提到的contentInset,并且还更改了文本字段底部约束的
常量
属性,使其在显示和隐藏键盘时向上移动和停止。

我假设由于您设置的自动布局约束,表视图的位置不正确。你应该从深入研究开始。我真的不知道你所说的高度值是什么意思,坦白地说,是键盘高度-标签栏高度。它不起作用,我也遇到了同样的问题。当我更改约束时,tableView框架已经更改。如果我加上不应该出现的收缩。不,你不需要改变框架,只需要改变内容插入。根本不更改约束,只需修改插入,如我发布的示例所示。由于scollindicator放置正确,表的自动布局是正确的。它正在工作。但是如何向上移动我的imageView…(我没有获得imageView的contentInset)。我假设由于您设置的自动布局约束,表视图的位置不正确。你应该从深入研究开始。我真的不知道你所说的高度值是什么意思,坦白地说,是键盘高度-标签栏高度。它不起作用,我也遇到了同样的问题。当我更改约束时,tableView框架已经更改。如果我加上不应该出现的收缩。不,你不需要改变框架,只需要改变内容插入。根本不更改约束,只需修改插入,如我发布的示例所示。由于scollindicator放置正确,表的自动布局是正确的。它正在工作。但是如何将imageView向上移动…(我没有获得imageView的contentInset)。