当键盘出现在iPhone上时,表格视图中的两个文本字段(均在scrollview上)消失

时间:2012-08-08 17:49:54

标签: iphone ios uiscrollview uitextfield

在我的登录视图中,我在具有两个单元格的分组表视图中具有电子邮件和密码文本字段。我以编程方式添加了两个文本域:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
    _cell.backgroundColor =  [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    if (indexPath.row == 0) {
        _emailTxtFld = [[UITextField alloc]initWithFrame:CGRectMake(10, 7, 277, 34)];
        _emailTxtFld.placeholder = @"E-mail";
        _emailTxtFld.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
        _emailTxtFld.clearsOnBeginEditing = YES;
        [_emailTxtFld setDelegate:self];
        [_emailTxtFld setKeyboardType:UIKeyboardTypeEmailAddress];
        [_cell.contentView addSubview:_emailTxtFld];

    }

    if (indexPath.row == 1) {
        _passwordTxtFld = [[UITextField alloc]initWithFrame:CGRectMake(10, 7, 277, 34)];
        _passwordTxtFld.placeholder = @"Password";
        _passwordTxtFld.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
        _passwordTxtFld.secureTextEntry = YES;
        [_passwordTxtFld setDelegate:self];
        _passwordTxtFld.clearsOnBeginEditing = YES;
        [_cell.contentView addSubview:_passwordTxtFld];
    }

    return _cell;
    }

这是滚动视图的全部内容。触摸文本字段时,键盘会出现,滚动视图会显示滚动视图的其余部分。问题是文本字段和表格视图以及我键入的文本消失,当我触摸它们进行编辑时。我仍然可以看到自动更正,在我键入内容并且键盘返回后,一切都恢复正常,我可以看到我输入的文本。

如果我在keyboardDidShow方法中注释掉所有内容,那么表格视图和文本字段不会消失。但显然我想保留这条线。

    -(void)keyboardDidShow:(NSNotification *)notif
    {
       _logScrollView.frame = CGRectMake(0, 0, 320, 245);// This line commented out stops the problem


    }

    -(void)keyboardDidHide:(NSNotification *)notif
    {
        _logScrollView.frame = CGRectMake(0, 0, 320, 460);
    }

如果我需要再提供一些代码,请告诉我。 谢谢你的回答!

2 个答案:

答案 0 :(得分:1)

你在scrollview中有一个tableview吗? UITableView已经是UIScrollView的子类。你不应该在另一个里面。

另一件事:你有这个:

_emailTxtFld.clearsOnBeginEditing = YES;

_passwordTxtFld.clearsOnBeginEditing = YES;

这意味着一旦文本字段成为第一个响应者,它们就会清除。键盘解散后,我真的不明白发生了什么。你能详细介绍一下正在消失的东西吗?

答案 1 :(得分:0)

我找到了解决问题的方法。我不知道正确的措辞,但我刚从我的tableview中创建了一个变量,我已经在IB中创建了一个变量,并且在keyboardDidShow方法中我使框架保持原样:

-(void)keyboardDidShow:(NSNotification *)notif
{
    _logScrollView.frame = CGRectMake(0, 0, 320, 245);

    _tableViewWTxtFlds.frame =  CGRectMake(8, 153, 304, 79);


}

-(void)keyboardDidHide:(NSNotification *)notif
{
    _logScrollView.frame = CGRectMake(0, 0, 320, 460);

    _tableViewWTxtFlds.frame =  CGRectMake(8, 153, 304, 79);
}

显然桌面视图已经离开了我的观点,如果这样修复的话,那就是我猜错了。谢谢你查看我的问题。