Skip to content

Instantly share code, notes, and snippets.

@linktoming
Forked from philipmcdermott/KeyboardHandler.m
Last active August 29, 2015 14:14
Show Gist options
  • Save linktoming/ed7343006c9ffcb47199 to your computer and use it in GitHub Desktop.
Save linktoming/ed7343006c9ffcb47199 to your computer and use it in GitHub Desktop.
How to use keyboard notification to adjust view when keyboard is about to show or hide
- (void)viewDidAppear:(BOOL) animated {
[super viewDidAppear:animated];
// Register notification when the keyboard will be show
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[notification.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
} completion:^(BOOL finished) {
}];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[notification.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
} completion:^(BOOL finished) {
}];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// Tutorial: http://spin.atomicobject.com/2014/01/08/animate-around-ios-keyboard/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment