2

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?

David
  • 21,070
  • 7
  • 57
  • 113
  • You may wish to see http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1 – rmaddy Nov 13 '14 at 05:15
  • @rmaddy Yeah that's the question that I looked up that inspired me to write a catch-all for any text field. I figured it out though. Thanks for the help! :) – David Nov 13 '14 at 05:16

3 Answers3

1

Sorry, but no there's no way to do that. You have to do your animations or whatever you do for keyboard handling inside the notification observer. Here's what Apple recommends and works fine for me in all cases I've encountered upto now.

Apple's Keyboard Guide

Libran Coder
  • 769
  • 4
  • 11
1

For anyone else who is trying to do this, I found a way:

Maintain a weak reference to the currently editing text field:

UITextField* editingTextField;

- (void)textFieldDidBeginEditing:(UITextField*)textField
{
    editingTextField = textField;
}

- (void)textFieldDidEndEditing:(UITextField*)textField
{
    editingTextField = nil;
}

Then just reference the weak text field while it is open in the keyboardWillShow: method to calculate the appropriate offsets and move the view accordingly.

- (void)keyboardWillShow:(NSNotification*)notification
{
    //Use editingTextField's frame here
}
David
  • 21,070
  • 7
  • 57
  • 113
0

Simply use IQKeyboardManager. It is a single line code for all textfield in any view.Link: https://github.com/hackiftekhar/IQKeyboardManager

Amrit Sidhu
  • 1,663
  • 1
  • 14
  • 31