1. 添加键盘上升监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIKeyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
2.设置触发事件
- (void)UIKeyboardWillChangeFrame:(NSNotification *)note
{
NSDictionary *userInfo = note.userInfo;
//得到note内容
CGFloat animationTime = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
CGRect keyRect = [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//设置动画
[UIView animateWithDuration:animationTime animations:^{
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
CGRect tempF = self.view.frame;
tempF.origin.y = keyRect.origin.y - screenH;
self.view.frame = tempF;
}];
}