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

来源:互联网 发布:前两年悲伤的网络歌曲 编辑:程序博客网 时间:2024/06/05 21:12

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


- (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.csdn.net/dengbin9009/article/details/12706633,另发现不合理的地方还望大家即时指出。