@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置代理
self.inputView.delegate = self;
self.tableView.delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)keyboardWillChangeFrameNotification:(NSNotification *)notification{
// 键盘消息机制配置打印,用来测试、查看键盘属性
NSLog(@"%@", notification.userInfo);
CGRect keyFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 设置新Y点,计算方法为,键盘的Y点 - 视图高度 = 视图新y点
CGFloat y = keyFrame.origin.y - self.view.frame.size.height;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = y;
// 获取键盘动画时间,用以与view时间相同
CGFloat time = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 播放动画方法
[UIView animateWithDuration:time delay:0 options:7<<16 animations:^{
self.view.frame = viewFrame;
} completion:nil];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self.view endEditing:YES];
}
@end
UIKeyboardWillShowNotification
/
/
键盘即将显示
UIKeyboardDidShowNotification
//
键盘显示完毕
UIKeyboardWillHideNotification
/
/
键盘即将隐藏
UIKeyboardDidHideNotification
//
键盘隐藏完毕
UIKeyboardWillChangeFrameNotification
/
/
键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification
//
键盘的位置尺寸改变完毕