Last active
August 29, 2015 14:25
-
-
Save appleios/3c815beb12d43b99307a to your computer and use it in GitHub Desktop.
keyboard notification observers (ios)
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]; | |
[[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