Question

This is a problem of order of operations

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];    
[nc addObserver:self 
       selector:@selector(keyboardWillShow:) 
           name:UIKeyboardWillShowNotification 
         object:nil];
[nc addObserver:self 
       selector:@selector(keyboardWillHide:) 
           name:UIKeyboardWillHideNotification 
         object:nil];

And then I add a textbox to a UITableViewCell:

[textField addTarget:self 
              action:@selector(textFieldBegin:) 
    forControlEvents:UIControlEventEditingDidBegin];

[cell addSubview:textField];

In textFieldBegin, I scrollToRowAtIndexPath to move to the cell being edited.
In keyboardWillShow I adjust the frame of the tableView to allow for the keyboard.
textFieldBegin gets called before keyboardWillShow, so the first time it is shown it has no room to scroll.

Is there an elequent way to fix this oversight?

Was it helpful?

Solution

You could store the current scroll position in a variable during begin, then in the keyboardwillshow notification you could re-scroll to whatever position is stored in that variable.

I think that would allow you to keep your different animations where they belong.

OTHER TIPS

Instead of listening for the UIKeyboardWillShowNotification you can react to the UITextFieldDelegate method textFieldDidBeginEditing: and resize/scroll in that method, since it is called after the keyboard is shown.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top