I have a couple UITextFields that will be hidden by the keyboard when it pops up, so I implemented the UIKeyboardWillShowNotification
and UIKeyboardWillHideNotification
and sent them to their respective methods:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
keyBoardHeight = height;
}
- (void)keyboardWillHide:(NSNotification*)notification
{
CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
keyBoardHeight = height;
}
I then have my textfields delegating to this method:
//TextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
float keyboardOffset = [self offsetForKeyboard:textField.frame.origin.y withHeight:textField.frame.size.height];
NSLog(@"%f, %f", keyBoardHeight, keyboardOffset);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
self.view.frame = [Global rect:self.view.frame withNewY:-[self offsetForKeyboard:textField.frame.origin.y withHeight:textField.frame.size.height]];
[UIView commitAnimations];
}
The code runs properly, but when I run the code, the keyboardWillShow:
method runs after the textFieldDidBeginEditing:
method. Because of this, keyboardHeight
is set to 0 the first time the code runs, and so the offset I calculate is way off.
How can I make the keyboardWillShow:
method run before the textFieldDidBeginEditing:
method? Is there another delegate method that I can use instead?