IOS 键盘遮挡输入框 视图上移解决方案
2015-04-14 15:25
537 查看
开发的朋友也许会经常遇到点击输入框激活出现键盘而导致输入框被遮住的现象,常用的解决方案有两种:
1、键盘出现的时候,将内容页面适当上移
2、键盘出现的时候,漂浮输入框到适当位置 笔者认为 第一种方案比较简单,这里只介绍第一种,第二种类似。
一、首先对键盘事件进行监听设置(可写入viewDidLoad中):
//增加监听,当键盘出现或改变时收出消息
[[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*)aNotification { NSDictionary* info = [aNotification userInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//得到鍵盤的高度
keyboardhight=kbSize.height;
//将UISCROLLVIEW上移动 hashKeyBoard=YES;
//设置动画的名字
[UIView beginAnimations:@"AnimationOpen" context:nil];
//设置动画的间隔时间 [UIView setAnimationDuration:0.20];
//??使用当前正在运行的状态开始下一段动画
[UIView setAnimationBeginsFromCurrentState: YES];
//设置视图移动的位移
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardhight, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束 [UIView commitAnimations]; }
//下移就不赘述咯
1、键盘出现的时候,将内容页面适当上移
2、键盘出现的时候,漂浮输入框到适当位置 笔者认为 第一种方案比较简单,这里只介绍第一种,第二种类似。
一、首先对键盘事件进行监听设置(可写入viewDidLoad中):
//增加监听,当键盘出现或改变时收出消息
[[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*)aNotification { NSDictionary* info = [aNotification userInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//得到鍵盤的高度
keyboardhight=kbSize.height;
//将UISCROLLVIEW上移动 hashKeyBoard=YES;
//设置动画的名字
[UIView beginAnimations:@"AnimationOpen" context:nil];
//设置动画的间隔时间 [UIView setAnimationDuration:0.20];
//??使用当前正在运行的状态开始下一段动画
[UIView setAnimationBeginsFromCurrentState: YES];
//设置视图移动的位移
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardhight, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束 [UIView commitAnimations]; }
//下移就不赘述咯
相关文章推荐
- iOS 键盘遮挡输入框解决方案
- iOS键盘遮挡输入框,输入区域自动上移
- ios 当键盘出现遮挡视图 视图上移Bug
- iOS-监听键盘输入,视图整体上移或恢复-避免输入遮挡
- [Cordova/Phonegap] Cordova iOS 应用在第三方输入法的键盘弹出(点击输入框)时,页面不上移,导致输入框被键盘遮挡 的解决办法
- iOS 键盘遮挡输入框的解决方案
- 键盘弹出后输入框上移的解决方案
- iOS 键盘弹出视图上移
- iOS学习笔记——视图上移与键盘弹回
- IOS学习笔记36—解决键盘遮挡输入框(UITextField)问题
- 输入框被键盘遮挡时 让整个view上移
- IOS 实现键盘弹出视图上移功能
- IOS开发Swift语言键盘遮挡输入框(UITextField)
- H5 android 键盘弹起,输入框被遮挡解决方案
- ios 键盘遮挡输入框解决方法
- scrollview,键盘遮挡输入框,上移
- iOS TextField 弹出键盘时实现view整体上移下移:防止弹出键盘遮挡输入框
- swift3.0键盘弹起遮挡输入框问题的解决方案
- iOS 键盘遮挡输入框
- iOS学习笔记——视图上移与键盘弹回