Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate UIView Up With Keyboard [duplicate]

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?

like image 927
Nick Avatar asked Jun 07 '14 16:06

Nick


2 Answers

This is working for me:

- (void)keyboardWillShow:(id)keyboardDidShow
{
    [UIView beginAnimations:nil context:NULL];

    NSDictionary *userInfo = [keyboardDidShow userInfo];
    [UIView setAnimationDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
    [UIView setAnimationCurve:[userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
    [self.view layoutIfNeeded];

    [UIView commitAnimations];
}

- (void)keyboardWillHide:(id)keyboardDidHide
{
    [UIView beginAnimations:nil context:NULL];

    NSDictionary *userInfo = [keyboardDidHide userInfo];
    [UIView setAnimationDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
    [UIView setAnimationCurve:[userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];            
    [self.view layoutIfNeeded];

    [UIView commitAnimations];
}

UPDATE: Or you can do the same with blocks:

- (void)keyboardWillShow:(id)keyboardDidShow
{
    NSDictionary *userInfo = [keyboardDidShow userInfo];
    [UIView animateWithDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                          delay:0.f
                        options:[[keyboardDidShow userInfo][UIKeyboardAnimationCurveUserInfoKey] intValue] << 16
                     animations:^{
        ...
    } completion:^(BOOL finished) {
        ...
    }];
}

- (void)keyboardWillHide:(id)keyboardDidHide
{
    NSDictionary *userInfo = [keyboardDidHide userInfo];
    [UIView animateWithDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                          delay:0.f
                        options:[[keyboardDidHide userInfo][UIKeyboardAnimationCurveUserInfoKey] intValue] << 16
                     animations:^{
        ...
    } completion:^(BOOL finished) {
        ...
    }];
}
like image 182
electronix384128 Avatar answered Nov 15 '22 00:11

electronix384128


The notification userInfo dictionaries have both animation duration (UIKeyboardAnimationDurationUserInfoKey) and curve (UIKeyboardAnimationCurveUserInfoKey) information; if you use both of them, your animation should match the keyboard animation timing.

like image 24
Jesse Rusak Avatar answered Nov 14 '22 23:11

Jesse Rusak