处理输入框被键盘遮盖的问题
来源:互联网 发布:大数据hive 编辑:程序博客网 时间:2024/06/07 21:50
我们使用输入框类的控件,有时候在屏幕底部会出现键盘遮盖的问题。
有两种方式处理:1.利用代理方法;2.利用监听键盘事件;
利用代理
意思就是在代理方法里面进行对父视图的Y抽偏移量的计算,上代码缺点是位移不够精确。
#pragma mark - UITextFieldDelegate- (void)textFieldDidBeginEditing:(UITextField *)textField { NSLog(@"textFieldDidBeginEditing"); CGRect frame = textField.frame; CGFloat heights = self.view.frame.size.height; // 当前点击textfield的坐标的Y值 + 当前点击textFiled的高度 - (屏幕高度- 键盘高度 - 键盘上tabbar高度) // 在这一部 就是了一个 当前textfile的的最大Y值 和 键盘的最全高度的差值,用来计算整个view的偏移量 int offset = frame.origin.y + 42- ( heights - 216.0-35.0);//键盘高度216 NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width; float height = self.view.frame.size.height; if(offset > 0) { CGRect rect = CGRectMake(0.0f, -offset,width,height); self.view.frame = rect; } [UIView commitAnimations];}- (void)textFieldDidEndEditing:(UITextField *)textField { NSLog(@"textFieldDidEndEditing"); NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height); self.view.frame = rect; [UIView commitAnimations];}
利用监听键盘事件
推荐大家利用这种方式:
- (void)addNotification { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];}#pragma mark - Notification- (void)keyboardWillShow:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [aValue CGRectValue]; //父view的偏移量 = 选中textfield的Y + 选中textfield的Height - (父view的height - keyboar的height - keyboard的tabbar的height) CGFloat offset = (CGRectGetMaxY(self.editTextField.frame)+CGRectGetHeight(self.editTextField.frame))-(CGRectGetHeight(self.view.frame)-CGRectGetHeight(keyboardRect)); double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; if (offset > 0) { [UIView animateWithDuration:duration animations:^{ self.view.frame = CGRectMake(0.0f, -offset, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)); }]; }}- (void)keyboardWillHide:(NSNotification *)notification { double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; [UIView animateWithDuration:duration animations:^{ self.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)); }];}//获取当前正在编辑的textfield- (void)textFieldBeginEditing:(NSNotification *)notification{ self.editTextField = [notification object];}
总结
其实方法都是一样,最重要的是大家要理解重点,
父view的Y抽偏移量 =选中textfield的Y +选中textfield的Height -(父view的height - keyboar的height - keyboard的tabbar的height)
0 0
- 处理输入框被键盘遮盖的问题
- 移动端输入键盘被遮盖的处理方法
- 上移键盘的时候不遮盖输入框
- [Android]键盘弹出时输入框被遮盖的对策和注意点
- 使用UITextField 键盘遮盖输入框
- 键盘遮盖文本视图的处理
- 键盘遮盖问题
- IOS UITextField输入 键盘遮盖
- (十一)ReactNative 解决TextInput输入框被输入法遮盖问题
- mono touch中处理视图过长,输入框被键盘遮挡的问题
- activex插件遮盖问题的处理
- UITableView中的键盘遮盖问题
- IOS完美适配键盘高度,解决被遮盖问题的思想
- 键盘挡住输入框的处理方法
- Android编辑框被输入法遮盖,有效的处理办法
- Android编辑框被输入法遮盖,有效的处理办法
- [UI控件问题] UITextField控件处理键盘弹出时遮住输入框的问题
- 解决移动端输入法遮盖input输入框的问题…
- 14.4.6 Configuring Thread Concurrency for InnoDB 配置Thread 并发
- 在高速信号换层时打回流地VIA过孔,减小回流路径
- 画布画笔
- 修改ubuntu的apt-get软件源
- Android中简易天气预报的设计与实现 (1)
- 处理输入框被键盘遮盖的问题
- 爬虫
- Windows下zookeeper伪集群环境搭建
- 2016.6.27笔记(1)-多线程的wait,notify
- 数据库三范式解析
- java基础之异常6--finally讲解
- 通俗解释Fine-turning机制
- Android系统Intent中的Uri使用
- 微服务架构的优势与不足