通用方法解决UITextFiled输入的时候,键盘遮挡问题
我们在用键盘录入的时候,有可能会遮挡录入框,所以我们应调整UIView的位置,使其不被遮挡。我写了一个通用的方法可以解决这个问题:
1. [代码][C/C++]代码
- (void)moveView:(UITextField *)textField leaveView:(BOOL)leave
{
UIView *accessoryView = textField.inputAccessoryView;
UIView *inputview = textField.inputView;
int textFieldY = 0;
int accessoryY = 0;
if (accessoryView && inputview)
{
CGRect accessoryRect = accessoryView.frame;
CGRect inputViewRect = inputview.frame;
accessoryY = 480 - (accessoryRect.size.height + inputViewRect.size.height);
}
else if (accessoryView)
{
CGRect accessoryRect = accessoryView.frame;
accessoryY = 480 - (accessoryRect.size.height + 216);
}
else if (inputview)
{
CGRect inputViewRect = inputview.frame;
accessoryY = 480 -inputViewRect.size.height;
}
else
{
accessoryY = 264; //480 - 216;
}
CGRect textFieldRect = textField.frame;
textFieldY = textFieldRect.origin.y + textFieldRect.size.height + 20;
int offsetY = textFieldY - accessoryY;
if (!leave && offsetY > 0)
{ http://www.huiyi8.com/vi/
int y_offset = -5;
y_offset += -offsetY;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += y_offset;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
else
{ vi素材大全
CGRect viewFrame = CGRectMake(0, 20, 320, 460);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
2. [代码]用法很简单,在UITextFieldDelegate的两个方法里分别调用一下这个方法就OK了,如下示例:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self moveView:textField leaveView:NO];
}
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
[self moveView:textField leaveView:YES];
}
最新文章
- js的bind方法
- C#使用IrisSkin2.dll美化WinForm程序界面
- Objective-C 【NSString-字符串比较&;前后缀检查及搜索】
- java连接sqlserver2008报错 java.sql.SQLException: 对象名 '表名' 无效.
- Another Crisis
- 【动态规划】Vijos P1680 距离
- leetcode Letter Combinations of a Phone Number python
- php提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别
- [NOI 2007]社交网络
- 图片人脸检测——Dlib版(四)
- 朱晔和你聊Spring系列S1E6:容易犯错的Spring AOP
- asp.net中Request请求参数的自动封装
- web前端全栈学习之路
- Why-are-GPUs-well-suited-to-deep-learning
- java的GUI程序的基本思路是以JFrame为基础
- ArcGIS API for Silverlight——小滑块
- RHEL7系统管理之内核管理
- 【洛谷】1477:[NOI2008]假面舞会【图论】
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #3 如何编写内核模块
- [Exception Android 20] - Error:Execution failed for task ':app:processDebugResources'
热门文章
- IntelliJ IDEA 常用快捷键总结
- fastjson的序列化属性
- Linux内核源码分析--内核启动之zImage自解压过程
- maven打包自动配置数据库链接信息
- 【spring cloud】spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient
- Android 调用.so包时报错:No implementation found for native Lxxx, java.lang.UnsatisfiedLinkError: XXX时的解决办法(转)
- Examples osgparticleshader例子学习
- Android 适配器教程 (六)
- Cursor类用法:
- jquery $.proxy使用 Jquery实现ready()的源码