如何使在Cell中的TextFiled(TextView)自适应键盘高度

本文将介绍如何在使用Swift进行iOS应用开发时,当键盘弹出时自动调整TableView的滚动区域,确保用户仍然能够方便地与应用交互。通过实现键盘显示和隐藏的通知事件,我们能够计算键盘的高度并相应地调整滚动视图的显示区域,同时保持对滚动条的可见性。此外,文章还将讨论如何在键盘隐藏时复原TableView的状态,以及如何标记用户点击的文本字段所在的单元格矩形,从而在键盘覆盖时仍能定位到正确的输入位置。

首先必须在页面显示时加上键盘通知事件


- (void)viewWillAppear:(BOOL)animated
{
    // 设置键盘显示和隐藏通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}
当然不要忘记在页面显示时把事件注销掉

- (void)viewWillDisappear:(BOOL)animated
{
    // 注销键盘显示和隐藏通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

因为需要让TableView的显示区域缩小,所以要要使得TableView可以滑动,顺便看到滚动条

    _tableView.scrollEnabled = YES;

然后就是键盘的通知事件了

因为需要动画与键盘弹上同步所以需要键盘弹上的时间,同时获取键盘的Rect

    CGFloat duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

然后计算键盘的偏移量UIEdgeInsets 

如果你的TableView的底部离View的还有距离,则需要见键盘的高度上减去此距离

     NSInteger distance = 13;
     UIEdgeInsets e = UIEdgeInsetsMake(0, 0, bounds.size.height-distance, 0);

再加上动画效果

    [UIView animateWithDuration:duration animations:^{
        [_tableView setScrollIndicatorInsets:e];
        [_tableView setContentInset:e];
    }];
那么TableView的显示区域就设置好了,当然在取消键盘的时候TableView需要复原

-(void)keyboardWillHide:(NSNotification *)notification
{
    CGFloat duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [UIView animateWithDuration:duration animations:^{
        [_tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
        [_tableView setContentInset:UIEdgeInsetsZero];
    }];
    _tableView.scrollEnabled = NO;
}

看看效果发现TableView虽然可视区域正确,但是点击被键盘覆盖部分的Cell中的TextFiled却不会自动上弹,这是需要一个标记你所点击的TextFiled所在Cell的Rect

故此定义一个全局变量

    CGRect m_InputRect;             // 所选中的TextFiled的Rcct
设置textFiled的代理为self,并加上代理UITextFieldDelegate

    cell.textContext.delegate = self;
实现代理,目的就是找到TextFiled所在的Cell并且取得他的Rect,然后赋值给m_InputRec

<span style="font-size:24px">#pragma mark -UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    m_InputRect = CGRectZero;
    UIView* parent = textField.superview;
    while (parent) {
        if ([parent isKindOfClass:[UITableViewCell class]]) {
            m_InputRect = parent.frame;
            break;
        }
        parent = parent.superview;
    }
}</span>

所以最后在keyboardWillShow中加上

    [_tableView scrollRectToVisible:m_InputRect animated:YES];
即最后keyboardWillShow的实现为:

#pragma mark - Keyboard Notification
-(void)keyboardWillShow:(NSNotification *)notification
{
    CGFloat duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    UIEdgeInsets e = UIEdgeInsetsMake(0, 0, bounds.size.height - 13, 0);
    [UIView animateWithDuration:duration animations:^{
        [_tableView setScrollIndicatorInsets:e];
        [_tableView setContentInset:e];
        [_tableView scrollRectToVisible:m_InputRect animated:YES];
    }];
    _tableView.scrollEnabled = YES;
}

注明:如需转载请注明出处http://blog.youkuaiyun.com/dengbin9009/article/details/12706633,另发现不合理的地方还望大家即时指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值