弹出键盘tableview往上滚动与cell中的textField键盘回收

//监听键盘出现和消失

[[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];

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容