//======== 监听键盘的弹出事件 ======== [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; // 当键盘的位置大小发生改变时触发 - (void)keyboardWillChangeFrame:(NSNotification *)note { // 1. 获取键盘当前的高度 NSLog(@"%@", note.userInfo); CGFloat screenH = [UIScreen mainScreen].bounds.size.height; CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardY = rect.origin.y; CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; /// 2.将当前ViewController的View整体向上平移一个"键盘的高度" [UIView animateWithDuration:duration animations:^{ self.view.transform = CGAffineTransformMakeTranslation(0, keyboardY - screenH); }]; } ///记得移除通知 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }