ios - 静态 TableView - UITextField 被键盘隐藏

标签 ios uitableview uitextfield

也许这是一个反复出现的问题,但我被这个问题和一些 iOS 概念所困扰。我有一个ViewController,其中有静态 TableView 三个部分以及每个部分上的一些。在行内我有 UITextFields。我想要做的是防止键盘隐藏我的底部屏幕 UI 文本字段。我刚刚尝试了 Managing the Keyboard 中的苹果解决方案,但由于我没有得到附加到静态 TableView 的 ScrollView 背后的概念,所以我无法将这个想法实现到我的项目中。大家有推荐的地方可以学习吗?抱歉,如果无法解释我想要做什么。我有点失落。

任何帮助将不胜感激。

非常感谢, 马科斯。

最佳答案

我必须做类似的事情,这是我的代码,希望它对您有所帮助。

- (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);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    CGRect aRect = self.bounds;
    aRect.size.height -= kbSize.height;
    CGRect activeRect = [activeTextField convertRect:activeTextField.frame toView:self];

    if (!CGRectContainsPoint(aRect, activeRect.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeRect.origin.y-kbSize.height+10);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.activeTextField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.activeTextField = nil;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

此外,请确保在加载 View 时设置通知观察者,如下所示:

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

[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillBeHidden:)
                                                     name:UIKeyboardWillHideNotification object:nil];

只需将 activeTextField 定义为 UITextField 并确保您想要移动的所有内容都包含在您的 ScrollView 中(在您的情况下,您可以将 viewController 更改为 ScrollView )。另外,请确保您的scrollView的contentSize至少为self.bounds.size。

希望这有帮助。

关于ios - 静态 TableView - UITextField 被键盘隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13844581/

相关文章:

iphone - 制作 UITextField "appear"的最佳方法

swift - UITextField rightView 图像和按钮?

iphone - iPhone 中特定于设备的资源

objective-c - 如何使用btouch在BaiDuMaps for iOS中绑定(bind)@package字段?

iphone - 在自定义 UITableViewCell 中调整 UIImageView 的大小会产生不一致的结果

iphone - 解压缩图像以在 UITableView 上使用

ios - 当键盘出现时,如何阻止 tableView 外部的标题消失

swift - 当用户在 UItextfields 中快速输入时如何验证和更新模型

ios - UITableView 宽度动画时 UITableViewCell 中的 UIButton 跳转

ios - 仿射变换 + 演示应用程序