开发者

Scroll to selected textField with AQGridView (similar to tableView)

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜