- (void)viewDidLoad{
//监听通知中心
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
//设置做边距
self.inputView.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
//一直显示
self.inputView.leftViewMode = UITextFieldViewModeAlways;
} - (void)keyboardDidChangeFrame:(NSNotification *)noti{
NSLog(@”——–%@”,noti.userInfo);
//改变window的背景颜色
self.view.window.backgroundColor = self.tableView.backgroundColor;
// 键盘退出的frame
CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//键盘实时y
CGFloat keyY = frame.origin.y;
//屏幕的高度
CGFloat screenH = [[UIScreen mainScreen] bounds].size.height;
//动画时间
CGFloat keyDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
//执行动画
[UIView animateWithDuration:keyDuration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, keyY - screenH);
}];
}
//当tableview 滚动的时候 结束编辑事件 (退出键盘) - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self.view endEditing:YES];
}
// UIKeyboardAnimationCurveUserInfoKey = 7; 动画曲线动画
// UIKeyboardAnimationDurationUserInfoKey = “0.25”; 动画时间
// UIKeyboardBoundsUserInfoKey = “NSRect: {{0, 0}, {320, 216}}”; 键盘bounds
// UIKeyboardCenterBeginUserInfoKey = “NSPoint: {160, 588}”; 开始键盘的居中位置
// UIKeyboardCenterEndUserInfoKey = “NSPoint: {160, 372}”;结束键盘的居中位置
// UIKeyboardFrameBeginUserInfoKey = “NSRect: {{0, 480}, {320, 216}}”; 键盘开始弹出的frame
// UIKeyboardFrameChangedByUserInteraction = 0; 键盘改变frame
// UIKeyboardFrameEndUserInfoKey = “NSRect: {{0, 264}, {320, 216}}”; 退出键盘的frame