我已经搜索并搜索了这个问题,我承认我可能错过了一些东西,但我没有想法。
我在 UITableView 表中有一个 UITextField 在我的主类中页脚视图:
// Set table footer
PAPPhotoDetailsFooterView *footerView = [[PAPPhotoDetailsFooterView alloc] initWithFrame:[PAPPhotoDetailsFooterView rectForView]];
commentTextField = footerView.commentField;
commentTextField.delegate = self;
self.tableView.tableFooterView = footerView;
textField 在 PAPPhotoDetailsFooterView 类中设置如下:
commentField = [[UITextField alloc] initWithFrame:CGRectMake( 25.0f, 10.0f, 227.0f, 31.0f)];
commentField.font = [UIFont systemFontOfSize:kCommentCellContentLabelFontSize];
commentField.placeholder = @"Name photo";
commentField.returnKeyType = UIReturnKeySend;
commentField.autocapitalizationType = UITextAutocapitalizationTypeNone;
commentField.autocorrectionType = UITextAutocorrectionTypeNo;
commentField.textColor = [UIColor colorWithRed:73.0f/255.0f green:55.0f/255.0f blue:35.0f/255.0f alpha:1.0f];
commentField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[commentField setValue:[UIColor colorWithRed:154.0f/255.0f green:146.0f/255.0f blue:138.0f/255.0f alpha:1.0f] forKeyPath:@"_placeholderLabel.textColor"];
[mainView addSubview:commentField];
这是我的主类中的textFieldShouldReturn方法:
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Call method to store comment
NSString *trimmedComment = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[self parseAndStoreComment:trimmedComment];
// Reset the text field
[textField setText:@""];
return [textField resignFirstResponder];
}
还有我的键盘方法:
- (void)keyboardWillShow:(NSNotification*)note {
// Scroll the view to the comment text box
NSDictionary* info = [note userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
[self.tableView setContentOffset:CGPointMake(0.0f, self.tableView.contentSize.height-kbSize.height) animated:YES];
}
- (void)keyboardWillHide:(NSNotification*)note {
[self.tableView setContentOffset:CGPointMake(0.0f, self.tableView.tableFooterView.frame.origin.y) animated:YES];
}
正在发生的事情是,当我在键盘上按回车键时单击文本字段 -OR- 时,tableView 似乎滚动到我在键盘中设置的任何内容WillHide,但紧接着它会快速向上滚动到表视图完全向上和屏幕外的位置。
我尝试在keyboardWillHide
中将setContentOffset
的动画设置为 2.0f 秒,看看会发生什么,这将导致我指定的 contentOffset 滚动超过 2 秒,但紧接着它会在屏幕外拍摄。
在我的代码滚动表后,有些东西正在滚动表视图,但我不知道它是什么。
所以我意识到这只发生在模拟器上。我一直在模拟器和设备上测试该应用程序,但我想每次我尝试调试此问题时,我只在模拟器上进行了测试。
不知道为什么它会发生在模拟器上。