diff --git a/KLCPopup/KLCPopup.h b/KLCPopup/KLCPopup.h index 36fe364..aceb31d 100644 --- a/KLCPopup/KLCPopup.h +++ b/KLCPopup/KLCPopup.h @@ -123,6 +123,9 @@ extern const KLCPopupLayout KLCPopupLayoutCenter; // If YES, then popup will get dismissed when content view is touched. default = NO. @property (nonatomic, assign) BOOL shouldDismissOnContentTouch; +// If YES, then popup will move up or down when keyboard is on or off screen. default = NO. +@property (nonatomic, assign) BOOL shouldHandleKeyboard; + // Block gets called after show animation finishes. Be sure to use weak reference for popup within the block to avoid retain cycle. @property (nonatomic, copy) void (^didFinishShowingCompletion)(); diff --git a/KLCPopup/KLCPopup.m b/KLCPopup/KLCPopup.m index f297cce..cd40bfb 100644 --- a/KLCPopup/KLCPopup.m +++ b/KLCPopup/KLCPopup.m @@ -1036,22 +1036,55 @@ - (void)didChangeStatusBarOrientation:(NSNotification*)notification { #pragma mark - Subclassing - (void)willStartShowing { - + if (_shouldHandleKeyboard) { + [[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]; + + _containerView.center = CGPointMake(_containerView.superview.frame.size.width/2, _containerView.superview.frame.size.height/2); + CGRect frame = _containerView.frame; + if (up) { + frame.origin.y -= keyboardEndFrame.size.height/2; + } + + [UIView beginAnimations:nil context:nil]; + [UIView setAnimationDuration:animationDuration]; + [UIView setAnimationCurve:animationCurve]; + _containerView.frame = frame; + [UIView commitAnimations]; } @end