TableView-自动布局的键盘动画问题

Gee*_*eek 3 objective-c uitableview ios autolayout

我在视图中有一个tableView.tableView下面是一个工具栏(里面有textField),下面是tabbar.这个屏幕基本上是用来聊天的.

我在显示键盘时(降低高度)以及隐藏时(将高度增加回原始高度)更改视图的高度.显示键盘时,它工作正常.

问题是当键盘被隐藏时,tableView几乎没有一个混蛋.

问题不在于视图的动画,因为当我将延迟放在动画中时,tableView也会立即出现混蛋(甚至在视图动画开始之前).

显示键盘时:

在此输入图像描述

隐藏键盘时:

在此输入图像描述

显示键盘时动画降低高度的代码(此工作精细):

    // Remove constraint from view
    // Change constraint constant
    // Add constraint to view
    .
    .
    .

    [UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.view layoutIfNeeded];
    } completion:nil];

    // If at least one chat message
    if ([chatData count] != 0)
    {
        [chatTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([chatData count] - VALUE_ONE) inSection:VALUE_ZERO] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
Run Code Online (Sandbox Code Playgroud)

隐藏键盘时动画增加高度的代码:

    // Remove constraint from view
    // Change constraint constant
    // Add constraint to view
    .
    .
    .

    [UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.view layoutIfNeeded];
    } completion:nil];
Run Code Online (Sandbox Code Playgroud)

Roh*_*hit 7

而不是更改框架,只需在键盘出现时设置内容插入,并在隐藏键盘时使用表格的内容偏移.

tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, heightOfYourKeyboardAndStuff, 0.0);
[tableView setContentOffset: CGPointMake(0.0, -heightOfYourKeyboardAndStuff) animated: YES];
Run Code Online (Sandbox Code Playgroud)

&

[tableView setContentOffset: CGPointMake(0.0, preferedValue) animated: YES];
tableView.contentInset = UIEdgeInsetsZero;
Run Code Online (Sandbox Code Playgroud)

我认为它应该工作得很好和顺利.


Gee*_*eek 6

我看到它是由于tableView的高度变化.当键盘显示时,我正在减少并增加它.此外,我使用下面的方法滚动单元格代码,这正在改变contentOffset,它导致混蛋.

[chatTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([chatData count] - 1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
Run Code Online (Sandbox Code Playgroud)

解决方案是不要改变tableView的高度并使用下面的代码.

显示键盘时:

// Change table contentInset
UIEdgeInsets contentInset = self.chatTable.contentInset;
contentInset.bottom = keyboardHeight;

UIEdgeInsets scrollIndicatorInsets = self.chatTable.scrollIndicatorInsets;
scrollIndicatorInsets.bottom = keyboardHeight;

[UIView animateWithDuration:animationDuration animations:^{
    self.chatTable.contentInset = contentInset;
    self.chatTable.scrollIndicatorInsets = scrollIndicatorInsets;
}];

// Optional : Display last cell with animation
if ([chatData count] != 0)
{
    [chatTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([chatData count] - 1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

键盘隐藏时:

// Reset table contentInset
UIEdgeInsets contentInset = self.chatTable.contentInset;
contentInset.bottom = 0.0f;

UIEdgeInsets scrollIndicatorInsets = self.chatTable.scrollIndicatorInsets;
scrollIndicatorInsets.bottom = 0.0f;

[UIView animateWithDuration:animationDuration animations:^{
    self.chatTable.contentInset = contentInset;
    self.chatTable.scrollIndicatorInsets = scrollIndicatorInsets;
}];

// Optional : Display last cell with animation
if ([chatData count] != 0)
{
    [chatTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([chatData count] - 1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
Run Code Online (Sandbox Code Playgroud)