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.由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做: ...
随机推荐
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- iOS--手势之谜
原文转至: http://www.cnblogs.com/huangjianwu/p/4675648.html iOS在手机APP的手势操作中包含了:拖动.捏合.旋转.点按.长按.轻扫.自定义等等,详 ...
- #研发解决方案#基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案
郑昀 基于杨海波的设计文档 创建于2015/8/13 最后更新于2015/8/25 关键词:异常流量.rate limiting.Nginx.Apriori.频繁项集.先验算法.Lua.ELK 本文档 ...
- ORACLE数据库SQL语句的执行过程
SQL语句在数据库中处理过程是怎样的呢?执行顺序呢?在回答这个问题前,我们先来回顾一下:在ORACLE数据库系统架构下,SQL语句由用户进程产生,然后传到相对应的服务端进程,之后由服务器进程执行该SQ ...
- Attempt to fetch logical page (...) in database 2 failed. It belongs to allocation unit xxxx not to xxx
今天一个同事说在一个生产库执行某个存储过程,遇到了错误: Fatal error 605 occurred at jul 29 2014 我试着执行该存储过程,结果出现下面错误,每次执行该存储过程,得 ...
- VS2015 Git使用教程——优化项目编辑日志
一.前言 公司项目中,修改日志管理是一件很繁琐的事情,而且项目维护时间长了,会遗留下各种有用或无用的日志,对于有代码洁癖的人来说,无疑是一种灾难. 1.公司日志记录结构: 2.Git日志记录结构: 二 ...
- mysql 安装问题
针对免安装版的mysql: 1.启动CMD,在mysql安装目录下 ~\bin\目录下,输入: mysqld -install 安装msyql服务: 2.启动MySQL服务,输入: net st ...
- poj1006 / hdu1370 Biorhythms (中国剩余定理)
Biorhythms 题意:读入p,e,i,d 4个整数,已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i ,求n . (题在文末) 知识点:中国剩余定理 ...
- php-设置关键词高亮的字符串处理函数
/** * 设置关键词高亮的字符串处理函数 * @param [string] $str [要高亮的字符串] * @param array $word_arr [关键词] */function set ...
- jquery用一个事件控制另一个事件是否执行(不是删除事件)
想用click事件控制mouseover事件的执行,如果用删除绑定mouseover事件以后就不能再使用mouseover了,于是只需要设置一个全局变量,并赋值false,当点击click事件,将全局 ...