iOS 处理键盘遮挡TextField、TextView问题
之前处理键盘遮挡问题都是在每一个控制器进行单独处理,这样做真的是非常的费事,今天在做项目的时候就想到自己封装一个,记录一下这个“跌宕起伏”的过程。
思路是这样的:计算文本编辑控件Frame与键盘Frame,如果遮挡则移动控制器View。
创建控制器类:WKAvoidKeyboardViewController
#import <UIKit/UIKit.h> @interface WKAvoidKeyboardViewController : UIViewController @property (nonatomic, strong) UITextField *editTextField;
@property (nonatomic, strong) UITextView *editTextView; - (void)hideKeyboard:(NSNotification *)noti;
- (void)showKeyboard:(NSNotification *)noti; @end #import "WKAvoidKeyboardViewController.h" #define GetOSVersion [[UIDevice currentDevice].systemVersion floatValue] #define GetTransformDistance(Distance) (GetOSVersion < 7.1 ? Distance / 2 : Distance) @interface WKAvoidKeyboardViewController ()<UITextFieldDelegate, UITextViewDelegate> @end @implementation WKAvoidKeyboardViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
@end
步骤1:通过通知获取当前编辑的文本控件
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
- (void)textFieldBeginEditing:(NSNotification *)noti
{
self.editTextField = noti.object;
self.editTextView = nil;
}
- (void)textViewBeginEditing:(NSNotification *)noti
{
self.editTextView = noti.object;
self.editTextField = nil;
}
步骤2:通知获取键盘高度
步骤3:计算是否需要移动
#pragma mark - 键盘躲避 - (void)showKeyboard:(NSNotification *)noti
{
self.view.transform = CGAffineTransformIdentity;
UIView *editView = _editTextView ? _editTextView : _editTextField; CGRect tfRect = [editView.superview convertRect:editView.frame toView:self.view];
NSValue *value = noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"];
NSLog(@"%@", value);
CGRect keyBoardF = [value CGRectValue]; CGFloat animationTime = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
CGFloat _editMaxY = CGRectGetMaxY(tfRect);
CGFloat _keyBoardMinY = CGRectGetMinY(keyBoardF);
NSLog(@"%f %f", _editMaxY, _keyBoardMinY);
if (_keyBoardMinY < _editMaxY) {
CGFloat moveDistance = _editMaxY - _keyBoardMinY;
[UIView animateWithDuration:animationTime animations:^{
self.view.transform = CGAffineTransformTranslate(self.view.transform, , -moveDistance);
}]; }
} - (void)hideKeyboard:(NSNotification *)noti
{
// NSLog(@"%@", noti);
self.view.transform = CGAffineTransformIdentity;
}
初步试验:UITextFiled成功,然后到了UITextView,坑爹的问题粗线了=.=, UITextViewTextDidBeginEditingNotification 发送时间是在键盘弹出通知之后的,导致第一次点击TextView没有用,点击第二次才能产生效果。于是乎,我又开始尝试用TextView的Delegate来做,想当然的使用的代理方法
- (void)textViewDidBeginEditing:(UITextView *)textView
- (void)textViewDidBeginEditing:(UITextView *)textView
{ }
令人失望的是textViewDidBeginEditing:方法调用依然是在键盘通知弹出后再调用,此时心中想的是:哔了狗了,让人怎么玩!还是看看其他方法吧。于是在代理方法中看到了
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
尝试之后,此方法的确在键盘弹出前调用,大功告成,接下来就是设置代理的问题了
设置代理方法如下:
- (void)searchTextViewWithView:(UIView *)view
{
for (UIView *subview in view.subviews)
{
if ([subview isKindOfClass:[UITextView class]]) {
((UITextView *)subview).delegate = self;
}
if ([subview isKindOfClass:[UITextField class]]) {
((UITextField *)subview).delegate = self;
}
[self searchTextViewWithView:subview];
}
}
至此大功告成,使用方法:继承WKAvoidKeyboardViewController,如果是用故事版创建的文本控件,啥都不用做,如果是用代码创建的,则需要在ViewDidLoad中调用searchTextViewWithView方法
完整代码下载地址:https://github.com/WuKongCoo1/AvoidKeyboardDemo.git
iOS 处理键盘遮挡TextField、TextView问题的更多相关文章
- (iOS)关于键盘遮挡textfield问题
记录一下自己经常遇到问题.使用textfield(textview).当输入框位置比较靠下时,弹出的键盘会遮挡输入框,这是就需要动态移动输入框编辑状态时self.view的位置, 自己经常用的方法有两 ...
- iOS解决表格中TextField,TextView编辑时,输入框被键盘遮挡的问题
方法1:在原来的 UIViewController 内部再添加一层 UITableViewController 代码如下 : // // ViewController.m // 键盘遮挡问题 // / ...
- iOS避免键盘遮挡输入方案
项目中经常会遇到这样的问题:一个tableView中有大量的textField,当点击屏幕底部的textfield时,由于键盘弹出挡住了textfield输入框里的内容,造成很差的用户体验,如下图,点 ...
- 键盘遮挡控件(textfield/textview.......)
采用的是通知的常规方式 // 解决键盘遮挡问题//选择didShow是因为需要键盘的高度//选择willHide是因为视图frame重置需要优先于键盘消失,否则表现得不连贯 [[NSNotificat ...
- iOS开发笔记11:表单键盘遮挡、浮点数价格格式化显示、省市区选择器、View Debugging
1.表单键盘遮挡 应用场景为一个collectionView上有多个textfield.textView供用户填写信息. 之前输入项较少时,采取的方法比较粗暴,didSelectItemAtIndex ...
- iOS学习——键盘弹出遮挡输入框问题解决方案
在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发 ...
- 『零行代码』解决键盘遮挡问题(iOS)
关注仓库,及时获得更新:iOS-Source-Code-Analyze https://github.com/draveness/iOS-Source-Code-Analyze Follow: Dra ...
- iOS键盘遮挡输入框,输入区域自动上移
在iOS开发过程当中,遇到关于键盘遮挡输入框的问题,经过网络参考与实践,总结如下: 登录窗口,上下放置两个UITextField,一个用户名,一个密码,放置的在屏幕下方1/3处,当点击用户名时,自动弹 ...
- iOS键盘遮挡问题解决办法
iOS开发之“键盘遮挡输入框的解决办法”之一 -----键盘通知之前处理这种问题,总是在触发输入框编辑事件键盘弹出的时候,将当前的View整体向上移动,结束编辑又整体向下移,耗时耗力效率低. 在网上看 ...
随机推荐
- 基于本地iso 搭建的本地yum源 安装部署openldap
1,yum openldap-servers,openldap-clients 基于iso-cd1搭建的本地yum源(具体搭建参看ruige的repo本地快速搭建,在右边 找找看中输入repo key ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- RNTools
在使用RNTools的自定义功能加载bundle的时候, 记得要把 http:// 加上,否则加载bundle会找不到网络地址.
- Pods 更新后提示Bundle资源找不到
http://www.oschina.net/question/101347_2159145
- 如何将你牛逼的iOS代码分享到CocoaPod(转)
为了让读者一目了然,现在就从新建工程到最后提交podspec,简单粗暴的讲一通.Cocoapods不用解释太多,一句话...它是目前iOS开发中最广为使用的第三方库依赖管理工具. 下面开始讲创建pod ...
- cocos2d-x for wp8 设置横竖屏
在主project文件(xxx.cpp , xxx为你的项目名)中, 函数名为void xxx::SetWindow(CoreWindow^ window) 相关代码片例如以下: <pre na ...
- Haffman算法(C++)
Huffman编码,C++实现,只是为了说明大致的思路,还有很多不完美之处,比如在输入数据超出限制等条件下会出现错误. #include<iostream> #include<str ...
- window.onload()与$(document).ready()区别
浏览器加载完DOM后,会通过javascript为DOM元素添加事件,在javascript中,通常使用window.onload()方法. 在jquery中,则使用$(document).ready ...
- C# foreach获取集合元素索引的坑
,}; foreach(var prepareId in prepareIds) { Console.WriteLine(prepareIds.IndexOf(prepareId)); } 执行结果如 ...
- light oj 1027 A Dangerous Maze
一道数学期望题,唉唉,概率论学的不好啊~~ 求走出迷宫的期望时间. 由于有的门会回到起点,所以有可能会走很多遍,设能走出去的门的个数为n1,总的个数为n,那么一次走出去的概率为n1/n,走一次的用的平 ...