IOS 8 keyboard - issue pushing UITextField upwards when keyboard was shown
I have a message like a chat window. When the keyboard was shown, I would press the UITextField so that it is right above the keyboard. On 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;
}];
}
source to share
You are using UIKeyboardFrameBeginUserInfoKey
to make your code work you must useUIKeyboardFrameEndUserInfoKey
UIKeyboardFrameBeginUserInfoKey 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 the rotation factors applied to the contents of the windows as a result of the reorientation of the interface. Thus, you may need to convert the rectangle to window coordinates (using the convertRect: fromWindow: method) or view the coordinates (using the convertRect: fromView: method) before using it.
UIKeyboardFrameEndUserInfoKey A key for an NSValue object containing a CGRect that identifies the ending keyboard frame on screen coordinates. These coordinates do not take into account any rotation factors that are applied to the contents of the windows as a result of the reorientation interface. Thus, you may need to convert the rectangle to window coordinates (using the convertRect: fromWindow :) method, or view the coordinates (using the convertRect: fromView :) method before using it.
source to share