Michael
Michael

Reputation: 3739

iOS 8 Keyboard - Issue with pushing the UITextField up when keyboard was shown

I have a message like chat window. When the keyboard was shown, I would push the UITextField so it appears to be right above the keyboard. In iOS 7, it works fine, but it doesn't work on iOS 8. Here's my code

// Detect the keyboard events

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];

// Listen to keyboard shown/hidden event and resize. 

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.2f animations:^{

        CGRect frame = self.textInputView.frame;
        frame.origin.y -= kbSize.height;
        self.textInputView.frame = frame;

        frame = self.myTableView.frame;
        frame.size.height -= kbSize.height;
        self.myTableView.frame = frame;
    }];
    [self scrollToTheBottom:NO];
}


- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
     NSDictionary* info = [aNotification userInfo];
     CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.2f animations:^{

        CGRect frame = self.textInputView.frame;
        frame.origin.y += kbSize.height;
        self.textInputView.frame = frame;

        frame = self.myTableView.frame;
        frame.size.height += kbSize.height;
        self.myTableView.frame = frame;
    }];
}

Upvotes: 3

Views: 566

Answers (1)

shabbirv
shabbirv

Reputation: 9098

You're using UIKeyboardFrameBeginUserInfoKey, in order for your code to work, you should be using UIKeyboardFrameEndUserInfoKey

UIKeyboardFrameBeginUserInfoKey The key for an NSValue object containing a CGRect that identifies the start frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the window’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow: method) or to view coordinates (using the convertRect:fromView: method) before using it.

UIKeyboardFrameEndUserInfoKey The key for an NSValue object containing a CGRect that identifies the end frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the window’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow: method) or to view coordinates (using the convertRect:fromView: method) before using it.

Upvotes: 2

Related Questions