【问题标题】:UITableView slightly goes up when keyboard hides键盘隐藏时 UITableView 略微上升
【发布时间】:2014-03-06 11:59:30
【问题描述】:

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

        - (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)


(屏幕3)

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

【问题讨论】:

    标签: ios objective-c uitableview autolayout


    【解决方案1】:

    您的问题是当键盘出现时您正在更改表格视图框架,这是错误的。您需要更改 table view 的 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;
    }
    

    【讨论】:

    • 我假设由于您设置的自动布局约束,您的表格视图的定位不正确。你应该从深入研究开始。不太确定 height 的值是什么意思,坦率地说,它是键盘高度 - 标签栏高度。
    • 它不起作用,我遇到了同样的问题。当我更改约束时,tableView 框架已经更改。如果我添加不应该是这种情况的收缩。
    • 不,您不必更改框架,只需更改内容插图即可。根本不要更改约束,只需按照我发布的示例修改插图
    • 表的自动布局是正确的,因为scollindicator放置正确。
    • 它正在工作。但我怎样才能将我的 imageView 向上移动......(我没有得到 imageView 的 contentInset)。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 2018-03-26
      • 2020-10-12
      • 1970-01-01
      相关资源
      最近更新 更多