iOS KeyboardShow andd KeyboardHide
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)adjustViewByKeyboardState:(BOOL)isShow
keyboardInfo:(NSDictionary *)info {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
NSValue *keyboardBoundsValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardBounds;
[keyboardBoundsValue getValue:&keyboardBounds];
CGFloat keyboardHeight = keyboardBounds.size.height;
CGFloat offset;
if (isShow) {
offset = keyboardHeight - 120;
} else {
offset = 0;
}
CGRect listFrame = CGRectMake(0, -offset, self.view.frame.size.width,
self.view.frame.size.height);
[UIView beginAnimations:@"anim" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.2];
self.view.frame = listFrame;
[UIView commitAnimations];
}
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
[self adjustViewByKeyboardState:YES keyboardInfo:info];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
[self adjustViewByKeyboardState:NO keyboardInfo:info];
}