-(void)keyboardFrameChanged :(NSNotification *)notice
{
// NSDictionary *userInfo = notice.userInfo;
//
// CGRect newFrame = [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
// BOOL isRealChanege = ![NSStringFromCGRect(newFrame) isEqualToString :NSStringFromCGRect(self.oldKeyFrame) ];
// CGFloat moveY = 0;
// CGFloat oldY = 0;
// CGFloat newY = newFrame.origin.y;
//
// if([NSStringFromCGRect(CGRectZero) isEqualToString :NSStringFromCGRect(self.oldKeyFrame)])//首次传值
// {
// oldY = [userInfo[@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue].origin.y;
// }else{ //ios键盘传后续传值可能问题,直接采用自己之前保留的
// oldY = self.oldKeyFrame.origin.y;
// }
//
// moveY = newY - oldY;
// if (moveY && isRealChanege)
// {
// __weak typeof(self) weakSelf = self;
// [UIView animateWithDuration:[userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue] animations:^{
// CGRect frame = weakSelf.frame;
// weakSelf.frame = CGRectMake(frame.origin.x, frame.origin.y + moveY, frame.size.width, frame.size.height);
// }];
// self.oldKeyFrame = newFrame;
// }
//
1.在键盘第一次出现的时候保存view的旧frame值,
2.并移动到键盘的上面(根据view的高度以及键盘出现的y值计算view的y值,你也可以根据键盘移动的y值来设置contentsize的增长高度,前提是view的父视图的frame等于window的frame,否则自行转换frame)
3.当键盘消失的时候,设置oldFrame值。
if (newFrame.origin.y != ScreenSize.height) //键盘出现
{
if([NSStringFromCGRect(CGRectZero) isEqualToString :NSStringFromCGRect(self.oldFrame)])//首次传值
{
self.oldFrame = self.frame;
}
[UIView animateWithDuration:duration animations:^{
CGRect frame = weakSelf.frame;
frame.origin.y = newFrame.origin.y - weakSelf.frame.size.height;
weakSelf.frame = frame;
}];
}else{ //键盘隐藏
[UIView animateWithDuration:duration animations:^{
weakSelf.frame = weakSelf.oldKeyFrame;
}];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];