From bf6c89e76bb14d9ec98a47d54f6febb367d13eb9 Mon Sep 17 00:00:00 2001 From: Duc iOS Date: Tue, 28 Apr 2015 01:47:01 +0700 Subject: [PATCH] Add support for keyboard show/hide. --- KLCPopup/KLCPopup.m | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) 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