Created
December 10, 2015 06:25
-
-
Save victorchee/6cc132ec804065ae490b to your computer and use it in GitHub Desktop.
Handle keyboard notification
This file contains hidden or 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)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; | |
} | |
- (void)viewWillDisappear:(BOOL)animated | |
{ | |
[super viewWillDisappear:animated]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
#pragma mark - Notification | |
- (void)keyboardWillChangeFrame:(NSNotification *)sender | |
{ | |
NSDictionary *info = [sender userInfo]; | |
NSTimeInterval duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; | |
// Convert the keyboard frame from screen to view coorinates. | |
CGRect keyboardScreenBeginFrame = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue]; | |
CGRect keyboardScrrenEndFrame = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue]; | |
CGRect keyboardViewBeginFrame = [self.view convertRect:keyboardScreenBeginFrame fromView:self.view.window]; | |
CGRect keyboardViewEndFrame = [self.view convertRect:keyboardScrrenEndFrame fromView:self.view.window]; | |
CGFloat originDelta = keyboardViewEndFrame.origin.y - keyboardViewBeginFrame.origin.y; | |
[UIView animateWithDuration:duration | |
animations:^{ | |
} completion:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment