gpt4 book ai didi

ios - 处理键盘后面的 UITextField 的 2 个问题

转载 作者:行者123 更新时间:2023-12-01 16:49:13 24 4
gpt4 key购买 nike

在我的 View 中,我的 UITableViewCell 中有一个 UITextField。现在有可能 UITextField 可能在我的键盘后面,所以我使用以下两种方法正确处理。一个用于单击键盘时,另一个用于关闭键盘时:

- (void)keyboardNotification:(NSNotification*)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CustomCell *cell = (CustomCell*)[[tempTextField superview] superview];
CGRect textFieldRect = [cell convertRect:tempTextField.frame toView:self.view];
if (textFieldRect.origin.y + textFieldRect.size.height >= [UIScreen mainScreen].bounds.size.height - keyboardSize.height) {
thetableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0);
NSIndexPath *pathOfTheCell = [thetableView indexPathForCell:cell];
[thetableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:pathOfTheCell.row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

}

- (void)keyboardhideNotification:(NSNotification*)notification {
thetableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

现在它工作得有些好,但是有两个问题。
  • 如果 ENTIRE UITextField 低于键盘顶部,则 tableview 只会滚动到键盘上方。如果键盘位于被选中的 UITextField 的一半,则 tableview 将不会滚动到键盘上方。乍一看,这应该可以工作,但我可能会遗漏一些东西。

  • 2
    .当键盘位于键盘下方并且表格 View 向上滚动时,它会以一种很好的动画方式进行,但是当我单击完成时,它会立即弹回旧位置。我可以清楚地看到为什么会发生这种情况,但是我将如何制作漂亮的动画以恢复到 tableview 所在的旧位置?

    任何投入将不胜感激!

    更新:我设法找到了#1 的问题。这是一个愚蠢的错误。应该将 textField 的高度添加到原点,因为该测量值正在下降。现在进入#2 ...

    最佳答案

    此代码简单地修复了 1 和 2:

    - (void)keyboardNotification:(NSNotification*)notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CustomCell *cell = (CustomCell*)[[tempTextField superview] superview];
    CGRect textFieldRect = [tempTextField convertRect:tempTextField.frame toView:self.view];
    if (textFieldRect.origin.y + textFieldRect.size.height >= [UIScreen mainScreen].bounds.size.height - keyboardSize.height) {
    NSDictionary *info = [notification userInfo];
    NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    double duration = [number doubleValue];
    [UIView animateWithDuration:duration animations:^{
    thetableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0);
    }];
    NSIndexPath *pathOfTheCell = [thetableView indexPathForCell:cell];
    [thetableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:pathOfTheCell.row inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }

    }

    - (void)keyboardhideNotification:(NSNotification*)notification {
    NSDictionary *info = [notification userInfo];
    NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    double duration = [number doubleValue];
    [UIView animateWithDuration:duration animations:^{
    thetableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }];
    }

    关于ios - 处理键盘后面的 UITextField 的 2 个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17583292/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com