//監(jiān)聽鍵盤出現(xiàn)和消失
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
#pragma mark 鍵盤出現(xiàn)
-(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];
}
然后我添加頭部視圖的輕拍手勢, 實現(xiàn)點擊頭部視圖發(fā)送通知, 也可以回收鍵盤, 應(yīng)該有更有效率的方法
在頭部視圖的view的類中
//頭部視圖添加輕拍手勢, 實現(xiàn)回收鍵盤
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 - 通知實現(xiàn)方法, 回收鍵盤
- (void)callBackKeyboard:(NSNotification *)noti{
[_textFieldOfInfo resignFirstResponder];
}