键盘的弹出与消失(获得弹出与消失的状态)
2015-10-20 14:41
260 查看
首先需要在viewdidload内使用 如下代码,注册通知,并且使用 选择器方法,指明弹出与消失时候调用的方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
然后实现keyBoardWillShow:与keyBoardWillHide:方法:
-(void)keyBoardWillShow:(NSNotification * )notification{
if (!isKeyBoardShow) {
NSLog(@"键盘弹出啦");
NSDictionary * userInfo =[notification userInfo];
NSValue * value =[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyBoardRect=[value CGRectValue];
int height =keyBoardRect.size.height;
self.view.frame=CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height-height);
self.tableView.scrollEnabled=YES;
isKeyBoardShow=YES;
}
}
-(void)keyBoardWillHide:(NSNotification * )notification{
if (isKeyBoardShow) {
NSLog(@"键盘消失啦");
// _user.name=self.getName.text;
self.tableView.scrollEnabled=NO;
NSDictionary * userInfo =[notification userInfo];
NSValue * value =[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyBoardRect=[value CGRectValue];
int height =keyBoardRect.size.height;
self.view.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+height);
}
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
然后实现keyBoardWillShow:与keyBoardWillHide:方法:
-(void)keyBoardWillShow:(NSNotification * )notification{
if (!isKeyBoardShow) {
NSLog(@"键盘弹出啦");
NSDictionary * userInfo =[notification userInfo];
NSValue * value =[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyBoardRect=[value CGRectValue];
int height =keyBoardRect.size.height;
self.view.frame=CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height-height);
self.tableView.scrollEnabled=YES;
isKeyBoardShow=YES;
}
}
-(void)keyBoardWillHide:(NSNotification * )notification{
if (isKeyBoardShow) {
NSLog(@"键盘消失啦");
// _user.name=self.getName.text;
self.tableView.scrollEnabled=NO;
NSDictionary * userInfo =[notification userInfo];
NSValue * value =[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyBoardRect=[value CGRectValue];
int height =keyBoardRect.size.height;
self.view.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+height);
}
}
相关文章推荐
- int类型转换成String , 不足n位 在前面补0
- Android apk 的Zipalign优化
- Github进行fork后如何与原仓库同步
- 源码搭建LAMP环境
- MySQL常用语句小结
- Python 格式化字符总结
- 在地图页面,自动布局控件开始是隐藏或在屏幕外需要正常显示时再为正常的显示状态的,需要在显示之前加入
- openstack swift做glance后端存储
- Jackson框架转换时间格式不匹配的问题
- 单例模式 异常抛出
- find, xargs 遇到 特殊字符
- LaTeX 制作幻灯片
- Unreal引擎术语表
- IOS基本数据类型之枚举
- Android - Paint类介绍
- LintCode "Interval Sum II"
- VirtualBox安装Ubuntu12.04和Ubuntu14.04过程中,出现不能全屏的解决方案
- 如何在CoreOS集成Kubernetes核心组件Kubelet
- TOOLBAR和沉浸式状态栏入门
- HDOJ 2473 Junk-Mail Filter(并查集集合删点+虚拟父节点)