diff --git a/KLCPopup/KLCPopup.m b/KLCPopup/KLCPopup.m index f297cce..b41da99 100644 --- a/KLCPopup/KLCPopup.m +++ b/KLCPopup/KLCPopup.m @@ -1036,22 +1036,54 @@ - (void)didChangeStatusBarOrientation:(NSNotification*)notification { #pragma mark - Subclassing - (void)willStartShowing { - + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil]; } - (void)didFinishShowing { - + } - (void)willStartDismissing { - + } - (void)didFinishDismissing { - + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; +} + +#pragma mark - Keyboard notification handlers + +- (void)keyboardWillShowNotification:(NSNotification *)notification { + [self moveContainerViewForKeyboard:notification up:YES]; +} + +- (void)keyboardWillHideNotification:(NSNotification *)notification { + [self moveContainerViewForKeyboard:notification up:NO]; +} + +- (void)moveContainerViewForKeyboard:(NSNotification *)notification up:(BOOL)up { + NSDictionary *userInfo = [notification userInfo]; + NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; + UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]; + CGRect keyboardEndFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; + + CGRect frame = _containerView.frame; + if (up) { + frame.origin.y -= keyboardEndFrame.size.height/2; + } else { + frame.origin.y += keyboardEndFrame.size.height/2; + } + + [UIView beginAnimations:nil context:nil]; + [UIView setAnimationDuration:animationDuration]; + [UIView setAnimationCurve:animationCurve]; + _containerView.frame = frame; + [UIView commitAnimations]; } @end