当cell中有UItextfiled或者UITextVIew时,弹出键盘把tableview往上,但是有的cell没有移动...

cell中有UITextView时,输入文字是需要将tableView向上移,基本的做法是,注册键盘变化的通知在通知的方法中做tableVIew的位置调整,

一,一般做法

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
    NSLog(@"register");
}

 

- (void)keyboardWasShown:(NSNotification *)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    ///将内边距进行调整
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification {

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
}
今天做项目发现以上方法有一定缺陷,当tableView有多组的时候,有一组cell不跟着向上移动照成如下情况:

当键盘出现时,尾视图向上移动了但是这一组中的cell没有向上移动,这是什么原因,我至今没有搞清楚.

上述方法还会出现问题

解决办法找到了就是在键盘出现的通知方法中找到第一响应者(一般是:UITextField),判断它所在的cell是否显示在可见区域,如果没有则让tableVIew滑动至该cell全部可见:

#pragma mark 键盘出现

-(void)keyboardWillShow:(NSNotification *)note

{

    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    //取得第一响应者

    UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];  

    UIView *activeField = [keyWindow performSelector:@selector(firstResponder)]; 

    //使用了苹果的私有方法,将无法上传到App Store,下一篇介绍不使用该方法获取第一响应者

    //除去键盘后的页面显示区域

    CGRect aRect = self.view.frame;

    aRect.size.height =  aRect.size.height - keyBoardRect.size.height - 64;

 

///修改arect:

    CGRect tableReact = self.tableView.frame;

    float y = _tableView.contentOffset.y;

    tableReact.origin.y += y;

    tableReact.size.height -= keyBoardRect.size.height;

    aRect = tableReact;

 

    //判断cell底部是否在显示范围内

    CGPoint cellBottomPoint = CGPointMake(0.0, activeField.superview.superview.frame.origin.y +activeField.superview.superview.frame.size.height);

    if (!CGRectContainsPoint(aRect, cellBottomPoint) ) {

        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);

        CGPoint scrollPoint = CGPointMake(0.0, cellBottomPoint.y-aRect.size.height -44);

        [_tableView setContentOffset:scrollPoint animated:YES];

        

    }

    

}

#pragma mark 键盘消失

-(void)keyboardWillHide:(NSNotification *)note

{

     

    [UIView animateWithDuration:.35 animations:^{

       self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 45, 0);

    }];

}

这样就完美解决了以上问题;

 

转载于:https://www.cnblogs.com/duzhaoquan/p/8509540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值