【问题标题】:2 problems with dealing with UITextField behind Keyboard处理键盘后面的 UITextField 的 2 个问题
【发布时间】:2013-07-11 00:45:55
【问题描述】:

在我的视图中,我的 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);
}

现在它工作得有些好,但是有两个问题。

  1. 只有当整个 UITextField 低于键盘顶部时,tableview 才会滚动到键盘上方。如果键盘位于被选中的 UITextField 的一半,则 tableview 将不会滚动到键盘上方。乍一看,这应该可以工作,但我可能遗漏了一些东西。

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

任何意见将不胜感激!

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

【问题讨论】:

    标签: ios uitableview keyboard scroll uitextfield


    【解决方案1】:

    这段代码简单地修复了 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);
        }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多