也许这是一个反复出现的问题,但我被这个问题和一些 iOS 概念所困扰。我有一个ViewController,其中有静态 TableView 、三个部分以及每个部分上的一些行。在行内我有 UITextFields。我想要做的是防止键盘隐藏我的底部屏幕 UI 文本字段。我刚刚尝试了 Managing the Keyboard 中的苹果解决方案,但由于我没有得到附加到静态 TableView 的 ScrollView 背后的概念,所以我无法将这个想法实现到我的项目中。大家有推荐的地方可以学习吗?抱歉,如果无法解释我想要做什么。我有点失落。
任何帮助将不胜感激。
非常感谢, 马科斯。
最佳答案
我必须做类似的事情,这是我的代码,希望它对您有所帮助。
- (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);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.bounds;
aRect.size.height -= kbSize.height;
CGRect activeRect = [activeTextField convertRect:activeTextField.frame toView:self];
if (!CGRectContainsPoint(aRect, activeRect.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeRect.origin.y-kbSize.height+10);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.activeTextField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.activeTextField = nil;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
此外,请确保在加载 View 时设置通知观察者,如下所示:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
只需将 activeTextField 定义为 UITextField 并确保您想要移动的所有内容都包含在您的 ScrollView 中(在您的情况下,您可以将 viewController 更改为 ScrollView )。另外,请确保您的scrollView的contentSize至少为self.bounds.size。
希望这有帮助。
关于ios - 静态 TableView - UITextField 被键盘隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13844581/