彈出鍵盤tableview往上滾動與cell中的textField鍵盤回收

//監(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];

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容