我正在尝试创建一个简单的笔记块应用程序。我正在使用 AQGridView 在网格中显示注释块。每个注释块在 AQGridViewCell 的自定义子类中都有一个可编辑的 UITextField 属性(类似于 TableViewCell)。而且我在键盘处理方面遇到了一些问题。我已尝试遵循本指南http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html。我已经实现了这样的方法keyboardWillShow和keyboardWillBeHidden:
- (void)keyboardWillShow:(NSNotification *)notification
{
// Animates the done button.
[UIView beginAnimations:nil context:NULL];
// Display a done button
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(hideKeyboard)];
[[self navigationItem] setRightBarButtonItem:doneButton];
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.gridView.contentInset = contentInsets;
self.gridView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
NSLog(@"%f", activeField.frame.origin.y);
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y+kbSize.height);
[self.gridView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification *)notification
{
// Remove the "done" button
self.navigationItem.rightBarButtonItem = self.editButtonItem;
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.gridView.contentInset = contentInsets;
self.gridView.scrollIndicatorInsets = contentInsets;
}
我在 cellForItemAtIndex 中设置了 textField 的委托(类似于 cellForRowAtIndexPath)。
目前,滚动到正确的位置不能正常工作。我认为问题与文本字段有关,因为当我测试时:
// activeField is a UITextField property that I set in textFieldDidBeginEditing,
// and later set to nil in textFieldDidEndEditing.
NSLog(@"%f", activeField.frame.origin.y);
它总是返回相同的值,在我的情况下为 95,即使所选的 textField 不在同一行上。
有关如何解决此问题或其他解决方案的提示会很棒!