Skip to content

Instantly share code, notes, and snippets.

@urouro
Last active August 29, 2015 14:08
Show Gist options
  • Save urouro/eabb855a3af5c4d00710 to your computer and use it in GitHub Desktop.
Save urouro/eabb855a3af5c4d00710 to your computer and use it in GitHub Desktop.
Keyboard Show/Hide Events
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// キーボード表示・非表示イベント
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
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:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
#pragma mark - Notification
- (void)keyboardWillShow:(NSNotification *)notification
{
CGRect keyboardFrameEnd = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
__weak typeof(self)weakSelf = self;
[UIView animateWithDuration:0.3
animations:^{
CGRect frame = weakSelf.view.frame;
frame.origin.y = -(keyboardFrameEnd.origin.y / 4);
weakSelf.view.frame = frame;
}];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
__weak typeof(self)weakSelf = self;
[UIView animateWithDuration:0.3
animations:^{
CGRect frame = weakSelf.view.frame;
frame.origin.y = 0.0;
weakSelf.view.frame = frame;
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment