在我的登录 View 中,我在带有两个单元格的分组表格 View 中包含电子邮件和密码文本字段。我以编程方式添加了两个文本字段:
-(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;
}
这一切都在 ScrollView 上。当触摸文本字段时,键盘会出现,并且 ScrollView 会出现,并且可以通过滚动查看 View 的其余部分。问题是当我触摸文本字段和表格 View 以及我键入的文本时它们会消失。我仍然可以看到自动更正,在我输入一些内容并且键盘返回后,一切都恢复正常,我可以看到我输入的文本。
如果我注释掉 KeyboardDidShow 方法中的所有内容,那么 TableView 和文本字段不会消失。但显然我想保留这条线。
-(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);
}
如果我需要给您更多代码,请告诉我。 谢谢您的解答!
最佳答案
ScrollView 内有表格 View 吗? UITableView 已经是 UIScrollView 的子类。你不应该把一个放在另一个里面。
另一件事:你有这个:
_emailTxtFld.clearsOnBeginEditing = YES;
和
_passwordTxtFld.clearsOnBeginEditing = YES;
这意味着一旦文本字段成为第一响应者,它们就会被清除。我真的不明白键盘关闭后发生了什么。您能否详细说明正在消失的内容?
关于iphone - 表格 View 中的两个文本字段都在 ScrollView 上,当键盘出现时消失 iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11870259/