Skip to content

Instantly share code, notes, and snippets.

@appleios
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appleios/3c815beb12d43b99307a to your computer and use it in GitHub Desktop.
Save appleios/3c815beb12d43b99307a to your computer and use it in GitHub Desktop.
keyboard notification observers (ios)
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)notification
{
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
UIViewAnimationOptions options = (curve << 16) | UIViewAnimationOptionBeginFromCurrentState;
[self.view layoutIfNeeded];
self.editorViewBottomConstraint.constant = kbSize.height;
[UIView animateWithDuration:duration
delay:0.0
options:options
animations:^{
[self.view layoutIfNeeded];
}
completion:^(BOOL success){
[self.messagesTableViewController goToBottom];
}];
}
- (void)keyboardWillHide:(NSNotification*)notification
{
NSDictionary* info = [notification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[self.view layoutIfNeeded];
self.editorViewBottomConstraint.constant = 0;
[UIView animateWithDuration:duration
animations:^{
[self.view layoutIfNeeded];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment