iOS对键盘的处理
{
[textField resignFirstResponder];
}
由于UIViewController是继承自UIResponder的,所以可以覆写- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;这个开始触摸的方法来取消第一响应者,代码如下:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_tfPassword resignFirstResponder];
[_tfUsername resignFirstResponder];
}
- #import "ViewController.h"
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet UITextField *tfTest;
- @property (weak, nonatomic) IBOutlet UITextField *tfTest2;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- - (void)viewDidappear:(BOOL)animated
- {
- [super viewDidDisappear:animated];
- //移除键盘监听消息
- [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotificationobject:nil];
- [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotificationobject:nil];
- //注册键盘监听消息
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyShow:)name:UIKeyboardWillShowNotification object:nil];
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyHide:)name:UIKeyboardWillHideNotification object:nil];
- }
- //弹出键盘消息响应
- - (void)keyShow:(NSNotification *)no
- {
- NSLog(@"keyShow");
- NSDictionary *dic = [no valueForKey:@"userInfo"];
- CGFloat heightKeyboard = [[dicvalueForKey:@"UIKeyboardBoundsUserInfoKey"]CGRectValue].size.height;
- /*
- NSDictionary *userInfo = [aNotification userInfo];
- NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
- CGRect keyboardRect = [aValue CGRectValue];
- */
- UIView *firstResponderView = [self getFirstResponderView];
- CGFloat distanceToBottom = self.view.frame.size.height -CGRectGetMaxY(firstResponderView.frame);
- //动画
- if(distanceToBottom < heightKeyboard){
- CGRect frame = self.view.frame;
- frame.origin.y = -(heightKeyboard - distanceToBottom);
- [UIView animateWithDuration:0.3f animations:^{
- [self.view setFrame:frame];
- } completion:^(BOOL finished) {
- }];
- }
- }
- //关闭键盘消息响应
- - (void)keyHide:(NSNotification *)no
- {
- NSLog(@"keyHide");
- CGRect frame = self.view.frame;
- frame.origin.y = 0;
- //动画
- [UIView animateWithDuration:0.3f animations:^{
- [self.view setFrame:frame];
- } completion:^(BOOL finished) {
- }];
- }
- //点击背景区域自动隐藏键盘
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
- UIView *firstResponderView = [self getFirstResponderView];
- [firstResponderView resignFirstResponder];
- }
- //获取当前焦点所在的控件
- - (UIView *)getFirstResponderView{
- UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
- UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
- return firstResponder;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- @end
PS://获取当前编辑框相对于self.view的位置
CGRect frameRelative = [firstResponderView convertRect:firstResponderView.bounds toView:self.view];
iOS对键盘的处理的更多相关文章
- 【iOS自定义键盘及键盘切换】详解
[iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...
- iOS 收起键盘的几种方式
iOS 收起键盘的几种方式 1.一般的view上收起键盘 // 手势 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ ...
- ios 自定义键盘
由于项目需要,需要自定义键盘.ios系统键盘会缓存键盘输入,并保存在系统目录下的文件里,并且是明文存储,存在帐号密码泄漏风险.在别人代码基础上修改了下,美化了下界面,去掉了字符输入,加了点击特效,截图 ...
- IOS 回收键盘通用代码
感觉IOS的键盘回收好累,所以封装了一个通用一点的方法 -(IBAction)spbResignFirstResponder:(id)sender { // NSLogObj(sender); if ...
- iOS学习——键盘弹出遮挡输入框问题解决方案
在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发 ...
- iOS 解决键盘挡住输入框的问题
iOS开发中经常会用到输入框UITextField,所以也常会遇到键盘挡住输入框而看不到输入框的内容. 在这里记录一种方法,用UITextField的代理来实现View的上移来解决这个问题. 首先设置 ...
- ios键盘弹起 body的高度拉长,页面底部空白问题。ios软键盘将页面抵到上面后,关闭软键盘页面不回弹的问题。
js 监听ios手机键盘弹起和收起的事件 /* js 监听ios手机键盘弹起和收起的事件 */ document.body.addEventListener('focusin', () => { ...
- ios处理键盘的大小
iOS的键盘有几个通知 UIKeyboardWillShowNotification UIKeyboardDidShowNotification UIKeyboardWillHideNotificat ...
- iOS数字键盘自定义按键
UIKeyboardTypeNumberPad 数字键盘自定义按键 最近做一个搜索用户的功能,这里使用了UISearchBar.由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做: ...
随机推荐
- ListDefinition Tips
1)ListTemplate.Type位数不能太长(最长7位),否则启用内容类型后,列表设置中会抛异常. <ListTemplate Name="List1" Type=&q ...
- iOS之处理不等高TableViewCell的几种方法
课题一:如何计算Cell高度 方案一:直接法(面向对象) 直接法,就是把数据布局到Cell上,然后拿到Cell最底部控件的MaxY值. 第一步:创建Cell并正确设置约束,使文字区域高度能够根据文字内 ...
- Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池
前言:由于最近在做SDK的功能,需要设计线程池.看了很多资料不知道从何开始着手,突然发现了AsyncTask有对线程池的封装,so,就拿它开刀,本文将从AsyncTask的基本用法,到简单的封装,再到 ...
- Mac常用shell命令
几个常用命令 pwd 命令名称:pwd 英文:print work directory 描述:查看当前工作目录的完整路径 ls 英文全称: list 描述:列出目录下的内容清单 常用参数: -l:列出 ...
- 第一课 ionic 日志输出
写程序的首要问题就是要打印日志,因为只有将日志输出才能真正了解程序的运行状态. 日志输出有两种方式 1.console输出 console.log("测试一下") console. ...
- Lucene 时间排序
在Lucene4.4中,想要实现搜索结果按照时间倒序的效果:如果两个文档得分相同,那么就按照发布时间倒序排列:否则就按照分数排列.这种效果在Lucene4.6中实现起来极其简单,直接利用search接 ...
- 《javascript权威指南》读书笔记——第一篇
<javascript权威指南>读书笔记——第一篇 金刚 javascript js javascript权威指南 由于最近想系统学习下javascript,所以开始在kindle上看这本 ...
- 如何给不支持新特性的浏览器打补丁(让老版本IE兼容新特性)
一个非常棒的 JavaScript 框架叫做 Modernizr(http://www.modernizr. com),用于向缺少 HTML5/CSS3特性支持的浏览器打补丁.由 Alexander ...
- ns3重要类
1. simple-ref-count 递归模板,使得通过定义T:SimpleRefCount<T>使得T类都具有计数功能 template <typename T, typenam ...
- ajax参数设置略解
通过ajax可以直接由页面访问到服务器.做到不刷新页面,就能刷新数据,为开发带来很大的便利. 1.ajax方式的参数及其功能: $.ajax({ type : "POST", // ...