轻松搞定UITableView中的键盘处理

 

 

 

 

代码如下:

1,通知:

#pragma mark - 键盘处理

-(void) keyboardWillShow:(NSNotification *)note{

    // get keyboard size and loctaion

    //键盘的frame

    CGRect keyboardBounds;

    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];

    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    

    //主窗口

    UIWindow* win = [UIApplication sharedApplication].keyWindow;

    DLog(@"%@",win);

    

    //_currentTF是cell中正在编辑的TF

    UITableViewCell* cell = [PCMNWE cellByTableView:self.tableView tf:_currentTF];

    NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];

    CGRect frame = [self.tableView convertRect:[self.tableView rectForRowAtIndexPath:indexPath] toView:win];

    DLog(@"%f",keyboardBounds.size.height);

    CGFloat height = frame.origin.y+44.0;

    if ((height + keyboardBounds.size.height) > UI_SCREEN_HEIGHT) {

        CGRect frameNew = self.tableView.frame;

        frameNew.origin.y -= keyboardBounds.size.height - (UI_SCREEN_HEIGHT - height);

        [UIView animateWithDuration:0.2 animations:^{

            self.tableView.frame = frameNew;

        }];

    }

    

}

 

2,代理:

- (void)textFieldDidEndEditing:(UITextField *)textField {

    

    //将self.tableView.frame打回原形

    [UIView animateWithDuration:0.3 animations:^{

        self.tableView.frame = CGM(0,0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - UI_STATUS_BAR_HEIGHT - 44.0);

    }];

}

 

 

暂时没有发现这段代码有什么问题.

若有BUG,欢迎加QQ指教:771722918

 

转载于:https://www.cnblogs.com/iOS771722918/p/4363829.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值