1

我在 tableview 上有 10 个禁用 tableview 滚动的文本字段。

如何处理键盘避免文本字段

以下是我尝试过的

//当键盘显示时注册通知

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

pragma mark - 键盘通知(在发送 UIKeyboardDidShowNotification 时调用。)

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    guestRechargeTableView.contentInset = contentInsets;
    guestRechargeTableView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;

    if (!CGRectContainsPoint(aRect, activeField.frame.origin) )
 {

        [guestRechargeTableView scrollRectToVisible:activeField.frame animated:YES];

    }
}

//在发送 UIKeyboardWillHideNotification 时调用

- (void)keyboardWillBeHidden:(NSNotification*)aNotification

{

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    guestRechargeTableView.contentInset = contentInsets;
    guestRechargeTableView.scrollIndicatorInsets = contentInsets;

}

在文本字段上选择并向上移动时,这是可行的,但不正确。为此或任何其他替代答案提供解决方案..

快乐的编码......

4

0 回答 0