Skip to content

Instantly share code, notes, and snippets.

@i03nomura1y
Last active October 11, 2015 08:47
Show Gist options
  • Save i03nomura1y/3833020 to your computer and use it in GitHub Desktop.
Save i03nomura1y/3833020 to your computer and use it in GitHub Desktop.
iOS keyboard notification
- (void)setKeyboardNotification{
NSNotificationCenter *c = [NSNotificationCenter defaultCenter];
[c addObserver:self selector:@selector(notifKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[c addObserver:self selector:@selector(notifKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)unsetKeyboardNotification {
NSNotificationCenter *c = [NSNotificationCenter defaultCenter];
[c removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[c removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)notifKeyboardWillShow:(NSNotification *)aNotification {
NSDictionary* info = aNotification.userInfo;
// この view から見た座標系に変換する
CGRect keyboardFrameEnd = [self.view.window convertRect:[info[UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:self.view];
[UIView animateWithDuration:[info[UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0
options:[info[UIKeyboardAnimationCurveUserInfoKey] intValue]
animations:^{
self.lc_bottom.constant = self.view.bounds.size.height - keyboardFrameEnd.origin.y;
[self.view layoutIfNeeded];
}
completion:^(BOOL finished){
}
];
}
- (void)notifKeyboardWillHide:(NSNotification *)aNotification {
NSDictionary* info = aNotification.userInfo;
[UIView animateWithDuration:[info[UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0
options:[info[UIKeyboardAnimationCurveUserInfoKey] intValue]
animations:^{
self.lc_bottom.constant = 0;
[self.view layoutIfNeeded];
}
completion:^(BOOL finished){
}
];
}
- (void)closeSoftKeyboard:(UIGestureRecognizer*)recog {
[self.view endEditing:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment