iOS软键盘遮挡UITableView内文本框问题

1、注册
UIKeyboardDidShowNotification/UIKeyboardDidHideNotification通知。

-(id) initWithNibName:(NSString*)nibNameOrNil bundle:nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        // 写在这里,或者viewDidLoad
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardDidHideNotification object:nil];
    }
    return self;
}

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

2、当通知到来,调整frame。

-(void) keyboardShown:(NSNotification*) notification {
    _initialTVHeight = _tableView.frame.size.height;

    CGRect initialFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect convertedFrame = [self.view convertRect:initialFrame fromView:nil];
    CGRect tvFrame = _tableView.frame;
    tvFrame.size.height = convertedFrame.origin.y;
    _tableView.frame = tvFrame;
}

-(void) keyboardHidden:(NSNotification*) notification {
    CGRect tvFrame = _tableView.frame;
    tvFrame.size.height = _initialTVHeight;
    [UIView beginAnimations:@"TableViewDown" context:NULL];
    [UIView setAnimationDuration:0.3f];
    _tableView.frame = tvFrame;
    [UIView commitAnimations];
}

3、触发文本框,滚动tableView

-(void) textFieldDidBeginEditing:(UITextField *)textField {
    NSIndexPath* path = [NSIndexPath indexPathForRow:row inSection:section];
    [self performSelector:@selector(scrollToCell:) withObject:path afterDelay:0.5f];
}
-(void) scrollToCell:(NSIndexPath*) path {
    [_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
}

4、苹果给的解决方案是这样的:
https://developer.apple.com/library/prerelease/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

demo 下载链接:
http://git.oschina.net/changyou/MyScrollViewKeyBoardDemo/repository/archive/master

5、或许这个更好

- (void)viewDidLoad {

//  监听键盘的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];

 }

 #pragma mark - 键盘通知回调方法
/*
 UIKeyboardAnimationCurveUserInfoKey = 7;   键盘执行动画的节奏
 UIKeyboardAnimationDurationUserInfoKey = "0.25"; 键盘弹出动画执行时间
 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}"; 键盘尺寸
 UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}"; 键盘当前的位置
 UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";  键盘改变后中心的位置
 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}"; 键盘当前的frame
 UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}"; 键盘改变后的frame
 */
- (void)keyboardWillChangeFrameNotification:(NSNotification *) notification
{
//    NSLog(@"%@",notification.userInfo);
//  1.取出键盘当前的y坐标
    CGRect beginFrame =  [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect endFrame =  [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//  2.中间的差值
    CGFloat minus =  endFrame.origin.y - beginFrame.origin.y ;
//  3.根据差值改变控制器view的frame
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += minus;
    self.view.frame = viewFrame;

}

/**
 *  拖拽tableView方法
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//  退出键盘
    [self.view endEditing:YES];
}

#pragma mark -dealloc方法写到最后
- (void)dealloc
{
//  移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

参考:

  1. http://blog.seancarpenter.net/2012/10/15/scrolling-a-uitableview-when-displaying-the-keyboard/

  2. Managing Text Fields and Text Views

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值