繁体   English   中英

键盘隐藏了StoryBoard中的UITableView

[英]Keyboard hides UITableView in StoryBoard

我正在开发一款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];

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

添加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);
            }];


}

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

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

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

您可以使用UITableViewController类而不是UIViewController来解决您自己的问题。

TextBeginEditing中的一行代码

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

它会使你的那一行排在最前面有很多位置,你可以相应地改变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM