【问题标题】:Two TextFields in tableview, all on scrollview, disappear when keyboard comes up iPhonetableview 中的两个 TextFields,都在滚动视图上,当键盘出现 iPhone 时消失
【发布时间】:2012-08-08 17:49:54
【问题描述】:

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

 -(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);
    }

如果我需要给你更多代码,请告诉我。 感谢您的回答!

【问题讨论】:

  • 声望超过十个我就加图
  • 它可能正在滚动到视野之外。您可以向上或向下滚动并“找到”文本字段吗?
  • 是的,我认为你是对的。我无法在任何地方看到文本字段。我在下面的回答解决了这个问题。

标签: iphone ios uiscrollview uitextfield


【解决方案1】:

你在滚动视图中有一个表格视图吗? UITableView 已经是 UIScrollView 的子类。你不应该在另一个里面有一个。

另一件事:你有这个:

_emailTxtFld.clearsOnBeginEditing = YES;

_passwordTxtFld.clearsOnBeginEditing = YES;

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

【讨论】:

    【解决方案2】:

    我找到了解决问题的方法。我不知道正确的措辞,但我只是从我已经在 IB 中创建的 tableview 中创建了一个变量,并且在 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);
    }
    

    显然,tableview 正在移出我的视图,如果修复它,我猜这就是错误的。感谢您查看我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 2014-08-20
      相关资源
      最近更新 更多