UITextField 弹出键盘时遮盖住textField的问题
2015-10-16 19:34
651 查看
在开发中,遇到 textField输入框 靠屏幕下方, 或者 屏幕下方有一些button 时, 弹出键盘就会把这些控件遮盖住, 只要我们在弹出键盘时 改变self.view 的frame属性,就可以实现视图的上移下移问题.
在开始编辑时触发textField的代理方法(需要遵守textField的协议)
恢复view的frame方法
利用动画效果实现frame的改变
还有一个问题 就是当输入文字的时候会有文字提示,这个长条也会遮挡住textField, 只要在下面的方法中再次调用改变高度的方法就可以了.
在开始编辑时触发textField的代理方法(需要遵守textField的协议)
- (void)textFieldDidBeginEditing:(UITextField *)textField { [self changeHeight:230]; // 原来键盘高度216, 但是在我的模拟器上要比216长, 所以 自己定义了 }
return键收回键盘时 触发的方法 主要是靠[self recoverFrame] 来恢复frame的 - (BOOL)textFieldShouldReturn:(UITextField *)textField { // NSString *str = textField.text; // 利用str 来传值// NSLog(@"%@", str); [textField resignFirstResponder]; [self recoverFrame]; textField.text = nil; return YES; }
恢复view的frame方法
- (void)recoverFrame { self.webView.userInteractionEnabled = YES; 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]; }
利用动画效果实现frame的改变
- (void)changeHeight:(CGFloat)keyBoardHeight { CGRect frame = self.textFD.frame; float offset = frame.origin.y + 32 - (self.view.frame.size.height - keyBoardHeight); 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]; }
还有一个问题 就是当输入文字的时候会有文字提示,这个长条也会遮挡住textField, 只要在下面的方法中再次调用改变高度的方法就可以了.
// 当点击键盘弹出文字提示时 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange :(NSRange)range replacementString:(NSString *)string { [self changeHeight:265];// 自己定义一下新的高度 return YES; }
相关文章推荐
- js模块化编程 : require.js
- iOS开发系列之UI基础:设置PCH
- VS中报错 “ Error:未定义标识符 cvRodrigues2“
- 关于UIAlertController 的一些用法
- Android在其他线程中更新UI
- Hibernate错误:Unable to build the default Bean Validator Factory
- [UI]实用案例--Shape绘制实用圆圈
- intent,requesrcode,resultcode开发总结
- iOS将UIView转换成UIImageView
- druid简单教程
- 【ROS】Ubuntu12.04下ROS-Fuerte安装及工作环境配置
- java hashMap 查询效率非常高,看一下根据key找value,根据value找key
- Top 10 Java Serialization Interview Questions and Answers
- iOS中 FMDB第三方SQLite数据库 UI_20
- iOS中 FMDB第三方SQLite数据库 UI_20
- phpCAS::handleLogoutRequests()关于java端项目登出而php端项目检测不到的测试
- iOS UITableView 相关知识
- iOS开发之UIlabel多行文字自动换行 (自动折行)
- UIView hitTest:withEvent:方法流程
- iOS 中UUID唯一标记的使用