美文网首页推送
弹出键盘tableview往上滚动与cell中的textFie

弹出键盘tableview往上滚动与cell中的textFie

作者: NateLam | 来源:发表于2016-07-15 11:02 被阅读370次

//监听键盘出现和消失

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

}

相关文章

网友评论

  • ebay_Happy:这个方法不行啊,返回跟原来压根不一样了。。直接设置off偏移就可以
    ebay_Happy:我的业务是cell上面都是键盘。键盘覆盖cell以后,想在点击最下面的cell键盘滑动的高度高于弹出键盘,这样就不会覆盖。你的方法我试了试,不行
    NateLam:@beast文强 哪个方法不行

本文标题: 弹出键盘tableview往上滚动与cell中的textFie

本文链接:https://www.haomeiwen.com/subject/zfjyjttx.html