Skip to content

Instantly share code, notes, and snippets.

@victorchee
Created December 10, 2015 06:25
Show Gist options
  • Save victorchee/6cc132ec804065ae490b to your computer and use it in GitHub Desktop.
Save victorchee/6cc132ec804065ae490b to your computer and use it in GitHub Desktop.
Handle keyboard notification
- (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