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

极客

我在视图中有一个tableView。tableView下面是一个工具栏(其中包含textField),下面是标签栏。该屏幕基本上用于聊天。

我在显示键盘(减小高度)和隐藏键盘(将高度增加到原始高度)时更改视图的高度。显示键盘时,它可以正常工作。

问题是当键盘被隐藏时,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];
    }

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

    // 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];
极客

我看到这是由于tableView高度的变化。显示键盘时,我正在减少和增加它。另外,我正在使用下面的方法滚动通过代码的单元格,这是不断变化的contentOffset,这会导致混蛋。

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

解决方案是不更改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];
}

隐藏键盘时:

// 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];
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章