首页 > 其他 > 详细

UITableView键盘遮挡 自动上移

时间:2015-05-04 20:17:50      阅读:245      评论:0      收藏:0      [点我收藏+]

要实现这个功能  大家都知道监听键盘的响应事件是最好的


步骤1

监听键盘的响应和失去响应的两个事件

代码如下:

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


步骤2

实现监听的两个方法

代码如下:

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

}
- (void)keyboardWillHide:(NSNotification *)info
{
    _tableView.contentInset = UIEdgeInsetsMake(_tableView.contentInset.top, 0, 0, 0);
}


步骤3

移除上面的两个监听事件

代码如下:

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


去试一下吧   很好用的一个小功能  

UITableView键盘遮挡 自动上移

原文:http://blog.csdn.net/lixianyue1991/article/details/45483451

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!