Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for keyboard show/hide. #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions KLCPopup/KLCPopup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)();

Expand Down
41 changes: 37 additions & 4 deletions KLCPopup/KLCPopup.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down