-(void)viewDidLoad {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//注册键盘显示通知
[center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//注册键盘隐藏通知
[center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void) keyboardWillShow:(NSNotification *) notification{
NSDictionary *info;
CGSize kbSize;
double kbheight;
CGFloat Oversize;
double duraction;
info = notification.userInfo;
kbSize = [[info objectForKey: UIKeyboardFrameEndUserInfoKey]
CGRectValue].size;
kbheight = kbSize.height;
Oversize = (self.view.frame.size.height - kbheight) - (textfield.frame.origin.y + textfield.frame.size.height+5);
duraction = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
if (Oversize < 0){
[UIView animateWithDuration: duraction animations:^{
self.view.frame = CGRectMake(0.0f, Oversize, self.view.frame.size.width, self.view.frame.size.height);
}];
}
}
-(void) keyboardWillHide:(NSNotification *) notification{
double duraction = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duraction animations:^{
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}];
}