【发布时间】:2016-07-27 17:24:03
【问题描述】:
我是 iOS 新手。我正在制作一个聊天应用程序,所以我在UITableView 下方有一个UITextField,UITextField 和UITableView 在UIScrollView 内部(我已通过self.scrollView.scrollEnabled = NO; 禁用滚动视图中的滚动)。
当我点击UITextField 时,会显示键盘,所以我将UITextField 移动到键盘上方跟随Managing the Keyboard doc
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:self.activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.activeField = nil;
}
使用此代码,我的TextField 成功移至键盘上方
但问题 是:在TableView 中,当显示键盘时,我无法滚动查看某些项目顶部(约 2 项)。
例如,如果我的 tableview 有 10 个项目,当显示键盘时,我只能看到 8 个项目,2 个其余项目仍然存在但我无法滚动到它们
这是我的Demo Project
我认为UITableView 的内容插入可能是错误的,但我不知道如何修复它。
任何帮助将不胜感激
【问题讨论】:
标签: ios uitableview uiscrollview keyboard uitextfield