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.由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做: ...
随机推荐
- 使用dubbo分布式服务框架发布服务及消费服务
什么是DUBBO DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案. 准备工作 安装zookeeper ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服 ...
- MongoDB安装及配置成服务
最近接收了个新项目,这个项目用到了很多之前没用过的(MongoDB.Redis.MVC5+EF6 等等),以前只是看过别人用,自己从未尝试,唯独用了MVC2+EF4,可能是我落伍了,不扯了,进入正题. ...
- Linux SendMail服务启动慢总结
在 CentOS release 6.6 上启动sendmail服务时发现服务启动过程非常慢,基本上要耗费3分多钟.有点纳闷:什么原因导致sendmail启动这么慢?搜索了这方面的一些资料,结合自己的 ...
- C、C++: 引用、指针、实例、内存模型、namespace
// HelloWorld.cpp : Defines the entry point for the console application. // #include "stdafx.h& ...
- jquery本地上传预览扩展(隐藏上传控件单击图片上传支持ie!!)
我用到的原材料地址:http://www.cnblogs.com/leejersey/p/3660202.html 修改后: /// <reference path="../../Js ...
- Solr嵌套子文档的弊端以及一种替代方式
背景:在考察了多种工具后,我们决定使用solr来作为多标签用户管理体系的查询方案. 原计划:电话,call客,跟进等等记录上报到kafka,然后通过flume+morphline录入到solr中.每一 ...
- [Java入门笔记] 面向对象三大特征之:封装
了解封装 什么是封装? 在Java的面向对象程序设计中,有三大基本特征:封装.继承和多态.首先我们先来看看封装: 在Java的面对对象程序设计中,封装(Encapsulation)是指一种将函数功能实 ...
- Python单例模式
1.单例模式介绍 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时, 单例对象的类必须保证只有一个实例存在.许多时候整个系统只需要拥有一个 全局对象,这样有利于我们协调系统整体的行为 ...
- SQL SERVER 2014 各个版本支持的功能
转自:https://technet.microsoft.com/library/cc645993 转换箱规模限制 功能名称 Enterprise Business Intelligence Stan ...
- 记SQL SERVER一次诡异的优化
最近做的项目快上线了,在做了一次压力测试后发现了不少问题,基本都是因为数据量达到一定级别时(预测系统上线10年后的数据量)SQL查询超时,其中有些是因为索引缺失.有些是因为写法不好,这些在有经验的人眼 ...