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

标签 ios uitableview keyboard scroll uitextfield

在我的 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/

    相关文章:

    ios - 从一个导航 Controller 更改为另一个

    ios - 从自定义 UITableViewCell 读取编辑后的 ​​UITextField

    ios - 使用多个 UITextFields 关闭键盘?

    ios - 如何从键盘将动画 gif 复制到剪贴板?(iOS 8)

    ios - 在调用下一个方法之前代码未完成

    ios - NSLocaleUsesMetricSystem 在 iPad 上总是 YES

    ios - 禁用 WKWebView 打开链接以重定向到我的 iPhone 上安装的应用程序

    ios - 如何在不创建 *Cell.swift 文件的情况下访问 UITableViewCell 对象

    objective-c - EXC_BAD_ACCESS 在 heightForRowAtIndexPath iOS

    c++ - 在所有情况下将 keyPressEvent 传播到顶层 QWidget