//监听键盘出现和消失
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
#pragma mark 键盘出现
-(void)keyboardWillShow:(NSNotification *)noti {
CGRect keyBoardRect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
}
#pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification *)noti {
self.tableView.contentInset = UIEdgeInsetsZero;
}
以上感谢http://blog.csdn.net/yo_yo_yang/article/details/51384421 的分享
在VC中
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_tableView resignFirstResponder];
}
在cell中
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_textFieldOfInfo resignFirstResponder];
}
然后我添加头部视图的轻拍手势, 实现点击头部视图发送通知, 也可以回收键盘, 应该有更有效率的方法
在头部视图的view的类中
//头部视图添加轻拍手势, 实现回收键盘
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callBackKeyboard:)];
[self addGestureRecognizer:tap];
#pragma mark - 头部视图轻拍手势, 回收键盘
- (void)callBackKeyboard:(UIGestureRecognizer *)tap{
[[NSNotificationCenter defaultCenter] postNotificationName:@"callBackKeyboard" object:nil];
}
因为tableView的cell有textfield, 所以在cell类中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBackKeyboard:) name:@"callBackKeyboard" object:nil];
#pragma mark - 通知实现方法, 回收键盘
- (void)callBackKeyboard:(NSNotification *)noti{
[_textFieldOfInfo resignFirstResponder];
}