I have a requirement to add chat functionality into an upcoming project.
In the meantime I have been trying to implement what I expected to be the simple matter of having a UIView at the bottom of the screen which has a UITextView inside, which will animate up with the keyboard when the user taps on the UITextView.
I have it working, however unfortunately the animation for the keyboard is slightly behind that of the view above it. Here is my implementation so far:
Register the keyboard notifications:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
Keyboard notification method:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSInteger curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration delay:0 options:curve animations:^{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
_chatViewBottomConstraint.constant = keyboardFrame.size.height;
[self.view layoutIfNeeded];
} completion:nil];
}
Has anyone else done similar, and are able to provide a better solution for me?