【问题标题】:Keyboard hides UITableView in StoryBoard键盘在 StoryBoard 中隐藏 UITableView
【发布时间】:2014-03-21 07:03:41
【问题描述】:

我正在开发一个 iPhone 应用程序。我的ViewController 上有一个简单的UITableView。在UITableView 中,我在单元格中添加了UITextField。我的问题是当我单击特定单元格中的UITextField 时,键盘会隐藏整个UITableView。我已经搜索过它,但无法成功找到解决方案。我像这样添加UITextField

UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(250, 15, 60, 30)];
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.backgroundColor = [UIColor clearColor];
textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
textField.tag =TEXT_VIEW_TAG;
[cell.contentView addSubview:textField];

如果有人知道它的解决方案,请告诉我。

【问题讨论】:

  • 请提供您的表格视图数据源和委托方法的代码,以便更深入地了解您的问题。

标签: ios iphone objective-c uitableview storyboard


【解决方案1】:

添加UITableViewController 实际上可以自行处理。但是,如果您确实需要使用UITableView,但仍希望键盘不隐藏您的UITableView,那么您需要在键盘打开时向上移动您的UITableView。为此,您可以更改 UITextFieldBeginEditing 方法中的 y 位置,也可以使用 UIKeyboard 通知。您需要在 viewDidLoad 方法中添加以下代码行:

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

然后,您可以添加以下方法:

    - (void) myKeyboardWillHideHandler:(NSNotification *)notification {

            [UIView animateWithDuration:0.2 animations:^{
                YOUR_TBL_VIEW.frame = CGRectMake(YOUR_TBL_VIEW.frame.origin.x, YOUR_TBL_VIEW.frame.origin.y + 80, YOUR_TBL_VIEW.frame.size.width, YOUR_TBL_VIEW.frame.size.height);
            }];
        }

    - (void) myKeyboardWillShowHandler:(NSNotification *)notification {
            [UIView animateWithDuration:0.2 animations:^{
                YOUR_TBL_VIEW.frame = CGRectMake(YOUR_TBL_VIEW.frame.origin.x, YOUR_TBL_VIEW.frame.origin.y - 80, YOUR_TBL_VIEW.frame.size.width, YOUR_TBL_VIEW.frame.size.height);
            }];


}

【讨论】:

  • 非常感谢您...非常感谢您。终于解决了我的问题:)
【解决方案2】:

尝试在 textFieldDidBeginEditing 方法中将此行添加到您的代码中

[self.tableView setContentOffset:CGPointMake(0,textField.center.y-170) animated:YES];

这里170是适合我的值,你可以试试其他的。 希望它可能会有所帮助。

【讨论】:

    【解决方案3】:

    您可以参加 UITableViewController 课程而不是 UIViewController,这将自行解决您的问题。

    【讨论】:

      【解决方案4】:

      TextBeginEditing 中的一行代码

      [self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES];
      

      它会让你的那一行在上面有很多位置,你可以相应地改变。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        相关资源
        最近更新 更多