UITableView键盘遮挡

转载:http://www.jianshu.com/p/c3b0a628d017

UITableView键盘遮挡

字数524  阅读293  评论1 

概述

今天要分享的内容是tableView中使用textFiled键盘遮挡问题,正好做了这个就把它写出来了。
方法有很多,一个方法是使用tableViewController,可以自动偏移的。还有个方法是在textFiled的代理方法里面设置。我用了另一种方法:

代码

注册键盘出现消失通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

键盘将要出现的通知回调方法

- (void)keyboardWillShow:(NSNotification *)notification
{
    CGRect keyboardBounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, 0, keyboardBounds.size.height, 0);
}

键盘将要消失的通知回调方法

- (void)keyboardWillHide:(NSNotification *)notification
{
    self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, 0, 0, 0);
}

移除通知

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

补充

上面代码中可以看到contentInset这个属性,打印iOS7和iOS6的这个属性会发现,iOS7中self.tableView.contentInset.top的值是64,这个属性就解释了iOS7以后scrollview以及其子类在导航栏下面会有64点偏移。
contentInset这个属性我在项目中也用到了,一个一级页面需要隐藏导航栏,二级页面显示,但是tableView需要留出导航栏的位置(>64),就用到了这个属性设置偏移。
上面代码会有个问题,在iOS7中使用挺好使的,整个tableView滚动区域偏移到键盘上方,textFiled偏移到键盘上方。但是在iOS6中会发现,textFiled不会偏移到键盘上方,但是整个tableView还会在键盘上方。所以iOS6需要在- (void)keyboardWillShow:(NSNotification *)notification添加设置tableViewsetContentOffset代码。

CGFloat y = NAV_HEIGHT+self.bgScrollView.contentSize.height+keyboardBounds.size.height-SCREEN_HEIGHT;
[self.bgScrollView setContentOffset:CGPointMake(0, y) animated:YES];

上面代码仅作参考,使用还需要修改下y值。
不过我就一个页面setContentOffset,iOS6设备也不多了,前面也解决了iOS6上面键盘遮挡问题,只是需要自己手动滑一下。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值