键盘遮挡控件(textfield/textview.......)
2015-11-10 15:33
281 查看
采用的是通知的常规方式
触发的方法
最后移除通知
最终测试改方式只适用于输入控件比较少的界面,更多的可以使用gitHub上第三方
// 解决键盘遮挡问题 //选择didShow是因为需要键盘的高度 //选择willHide是因为视图frame重置需要优先于键盘消失,否则表现得不连贯 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHide:) name:UIKeyboardWillHideNotification object:nil];
触发的方法
-(void)keyboardWasShown:(NSNotification*)notification { //获取键盘高度 NSValue* value=notification.userInfo[UIKeyboardFrameBeginUserInfoKey]; CGFloat keyBoradHeight=[value CGRectValue].size.height; NSNumber* animationTime=notification.userInfo[UIKeyboardAnimationDurationUserInfoKey]; double time=[animationTime doubleValue]; //获取被遮挡控件距离controller底部的距离 float a=self.view4.frame.origin.y+self.view4.frame.size.height; float b = self.view.frame.size.height-a; if (b<keyBoradHeight) { //动画效果 [UIView animateWithDuration:time animations:^ { //键盘被挡住了 CGRect viewCGrect=self.view.frame; //视图应该上移所以是- viewCGrect.origin.y=viewCGrect.origin.y-(keyBoradHeight-b); [self.view setFrame:viewCGrect]; } completion:nil]; } } -(void)keyboardWasHide:(NSNotification*)notification { NSValue* value=notification.userInfo[UIKeyboardFrameBeginUserInfoKey]; CGFloat keyBoradHeight=[value CGRectValue].size.height; float a=self.view4.frame.origin.y+self.view4.frame.size.height; float b = self.view.frame.size.height-a; CGRect viewCGrect=self.view.frame; viewCGrect.origin.y=viewCGrect.origin.y+(keyBoradHeight-b); [self.view setFrame:viewCGrect]; }
最后移除通知
-(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; }
最终测试改方式只适用于输入控件比较少的界面,更多的可以使用gitHub上第三方
DaidoujiChen / DaiDodgeKeyboard
hackiftekhar / IQKeyboardManager
相关文章推荐
- c++(合并排序)
- 如何在IIS7或IIS7.5中导入导出站点及应用程序池. -摘自网络
- 关于安卓eclipse SDK下载不了的解决方法
- 01 (maven+SSH)网上商城项目实战之项目简介
- 从头到尾写SQL(一)
- C语言创建生成WAV音频文件
- 基于离散图模型的人工视觉简介
- JavaScript学习(三):JavaScript对象类型
- GNU hello代码分析
- MMDrawerController(第三方类库)侧边栏的使用
- c++(快速排序)
- 01 (maven+SSH)网上商城项目实战之项目简介
- 类似aaa?a=1&b=2&c=3&d=4,如何将问号以后的数据变为键值对
- 网上商城项目实战之需求分析
- oracle 11g 审计
- 类似aaa?a=1&b=2&c=3&d=4,如何将问号以后的数据变为键值对
- http协议
- mysql 博客源码
- C#调用非托管DLL时遇到“找不到指定的模块”问题解决方法
- c++(非递归排序)