使用 AQGridView 滚动到选定的文本字段(类似于 tableView)

发布于 2024-11-27 07:05:51 字数 2554 浏览 4 评论 0原文

我正在尝试创建一个简单的笔记块应用程序。我正在使用 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)。

目前,滚动到正确位置无法正常工作。我认为问题与 textField 有关,因为当我测试时:

// 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 不在同一行。

有关如何解决此问题或其他解决方案的提示会很棒!

I'm trying to create a simple note block application. I'm using AQGridView to display note blocks in a grid. Every note block have a editable UITextField property in a custom subclass of AQGridViewCell (similar to TableViewCell). And I have some problems with keyboard handling. I have tried to follow this guide, http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html. I have implemented the methods keyboardWillShow and keyboardWillBeHidden like this:

- (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;

}

I set the delegate of the textField in cellForItemAtIndex (similar to cellForRowAtIndexPath).

Currently, the scrolling to the right position doesn't work correctly. I think the problem have to do with the textField, because when I test:

// activeField is a UITextField property that I set in textFieldDidBeginEditing, 
// and later set to nil in textFieldDidEndEditing.
NSLog(@"%f", activeField.frame.origin.y);

It always returns the same value, in my case 95, even if the selected textField isn't on the same row.

Tips on how to solve this, or other solutions, would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我要还你自由 2024-12-04 07:05:51

我想我通过为单元格的文本字段设置一个标签,然后从该标签/索引获取单元格来解决它。然后根据单元格的位置而不是单元格内的文本字段计算键盘出现时的位置。

I think I solved it by setting a tag to the cell's textField, and then getting the cell from that tag/index. And then calculating the position when the keyboard appears based on the cell's position instead of the textField inside the cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文