Thursday, 6 August 2015

Keyboard height change handling.

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

        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(keyboardWillBeHidden:)
         name:UIKeyboardWillHideNotification
         object:nil];
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification *)aNotification {

    [self scrollControlBarTo:aNotification up:YES];

}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification *)aNotification {


     [self scrollControlBarTo:aNotification up:NO];

}


- (void)scrollControlBarTo:(NSNotification *)notification up:(BOOL)up
{

    CGRect keyboardBounds;
    NSDictionary *info = [notification userInfo];
    NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    double duration = [number doubleValue];
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
    [UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         containerBottom.constant = (up) ? 8+keyboardBounds.size.height : 8;
                         [self.view layoutIfNeeded];
                     } completion:nil];
}

No comments:

Post a Comment