iOS解决键盘弹出遮挡输入框问题
2016-04-07 12:32
766 查看
1、错误:
键盘遮挡输入框最常见的可能就是在登录界面了,无论有多少个textFiled,不论是在VC的任何位置。都有可能造成键盘弹出来时,把输入框挡住了。如图:
2、解决方法:
首先要写上UITextFieldDelegate代理协议,,,切记!!!1:添加文本输入框,申明两个属性
@property (nonatomic, weak) UITextField * userNameText; @property (nonatomic, weak) UITextField * userPwdText;
UITextField * userNameText = [AutolayoutView autolayoutTextFieldWithPlaceholder:@"账号"]; userNameText.delegate = self; [self.view addSubview:userNameText]; self.userNameText = userNameText; UITextField * userPwdText = [AutolayoutView autolayoutTextFieldWithPlaceholder:@"密码"]; userPwdText.delegate = self; [self.view addSubview:userPwdText]; self.userPwdText = userPwdText;
2:实现代理方法
此处主要解决// 当前点击textfield的坐标的Y值 + 当前点击textFiled的高度 - (屏幕高度- 键盘高度 - 键盘上tabbar高度)
// 在这一部 就是了一个 当前textfile的的最大Y值 和 键盘的最全高度的差值,用来计算整个view的偏移量
- (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]; }
3:点击空白处的时候让其回到原来位置
/** * textField 取消选中状态 * */ -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesBegan"); [self.view endEditing: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]; }
还有点击键盘的return键的时候恢复原状就要在
- (BOOL)textFieldShouldReturn:(UITextField *)textField;里头处理。
切记一定要判断当前的textile是否是你点击的self.userNameText了。在让他恢复原状,解决后效果图:
相关文章推荐
- YouKu iOS笔试题二
- YouKu iOS笔试题一
- RNCryptor IOS文件加密解密
- iOS部分知识点小结
- 关于iOS的强引用,弱引用及strong,retain,copy,weak,assignd的关系
- iOS KVC 和 KVO 详解
- ios-kvc\kvo 原理
- iOS-百度地图之LBS.云检索学习
- iOS KVC & KVO
- iOS 高级开发 KVC(一),ios开发kvc
- iOS开发之----KVC的使用
- ios开发之你真的了解了KVC吗?
- iOS中静态库的使用
- iOS9适配 之 关于info.plist 第三方登录 添加URL Schemes白名单
- iOS--点赞动画效果
- iOS9适配系列教程
- iOS--点赞动画效果
- iOS-获取当前网页的 url 和 title 和 html
- iOS 学习之 - 去除数组中的重复数据
- iOS 开发库