OGeek|极客世界-中国程序员成长平台

标题: ios - 动画后重新加载 UITableView 重置帧 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:29
标题: ios - 动画后重新加载 UITableView 重置帧

我一直被这个问题困扰,我真的很好奇为什么会这样。我使用下面显示的简单示例重新创建了我的问题。我在 View 底部放置了一个带有自定义 UITableViewCell(在单元格中心包含一个 UILabel)的 UITableView,顶部是一个 UISearchBar。

当键盘弹出时,我为 UITableView 设置动画以更改其框架以适应 UISearchBar 下方和键盘上方的空间。现在,当我输入任何搜索文本并重新加载 UITableView 时,UITableView 会在动画之前更改其帧。

最初 -

UITableView Frame: {{0, 223}, {320, 237}} - {{x, y},{width, height}}

动画后 -

UITableView Frame: {{0, 44}, {320, 436}} - {(x, y),(width, height)}

当我输入任何搜索文本时 - UITableView Frame: {{0, 223}, {320, 237}} - {{x, y},{width, height}}

我注意到这只发生在自定义 UITableViewCell 上。我四处寻找答案,但一无所获。我尝试将 UILabel 作为 subview 添加到自定义 UITableViewCell 中的 contentView ,但这没有用。所以我的问题是,如何防止 UITableView 将其框架恢复为原始值?

编辑:这是我在动画后更改 UITableView 框架的代码。

- (void)keyboardDisplayedNSNotification*)notification
{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardValue = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrame = [keyboardValue CGRectValue];

    CGRect frame = self.testTabl.frame;
    frame.origin.y = self.testSearchBar.frame.origin.y + self.testSearchBar.frame.size.height;
    frame.size.height = keyboardFrame.origin.y - frame.origin.y;

    [UIView animateWithDuration:0.3 animations:^{
        [self.testTabl setFrame:frame];
    }];

    NSLog(@"TableView Frame: %@", NSStringFromCGRect(self.testTabl.frame));
}



Best Answer-推荐答案


我不确定它是否解释了您所看到的行为,但您代码中的主要问题是您需要改用 UIKeyboardFrameEndUserInfoKey。像下面这样的东西应该可以工作:

- (void)keyboardDisplayedNSNotification*)notification
{
    NSDictionary* keyboardInfo = [notification userInfo];
    CGPoint endPoint = [[keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin;
    CGFloat duration = [[keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];

    CGRect frame = self.testTabl.frame;
    frame.size.height = [self.testTabl convertPoint:endPoint fromView:nil].y;

    [UIView animateWithDuration:duration animations:^{
        [self.testTabl setFrame:frame];
    }];

    NSLog(@"TableView Frame: %@", NSStringFromCGRect(self.testTabl.frame));
}

关于ios - 动画后重新加载 UITableView 重置帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16010312/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4