-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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