方法1. 使用<UITextFeildDelegate>,使用的UITextField示例 设置其Delegate为self,点击return按钮隐藏键盘。实现函数如下:
   - (BOOL)textFieldShouldReturn:(UITextField *)textField  
   {   
         [textField resignFirstResponder];  
         return YES;
   }  
 
 
方法2. 点击界面的其它空白地方隐藏
     由于UIViewController是继承自UIResponder的,所以可以覆写- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;这个开始触摸的方法来取消第一响应者,代码如下:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

[_tfPassword resignFirstResponder];

[_tfUsername resignFirstResponder];

}

 
以上两种方法是初学iOS的时候使用的。
 
3. 最好还是使用NotificationCenter的方法比较好,能获取键盘高度等详细信息
#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对键盘的处理的更多相关文章

  1. 【iOS自定义键盘及键盘切换】详解

    [iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...

  2. iOS 收起键盘的几种方式

    iOS 收起键盘的几种方式 1.一般的view上收起键盘 // 手势 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ ...

  3. ios 自定义键盘

    由于项目需要,需要自定义键盘.ios系统键盘会缓存键盘输入,并保存在系统目录下的文件里,并且是明文存储,存在帐号密码泄漏风险.在别人代码基础上修改了下,美化了下界面,去掉了字符输入,加了点击特效,截图 ...

  4. IOS 回收键盘通用代码

    感觉IOS的键盘回收好累,所以封装了一个通用一点的方法 -(IBAction)spbResignFirstResponder:(id)sender { // NSLogObj(sender); if ...

  5. iOS学习——键盘弹出遮挡输入框问题解决方案

    在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发 ...

  6. iOS 解决键盘挡住输入框的问题

    iOS开发中经常会用到输入框UITextField,所以也常会遇到键盘挡住输入框而看不到输入框的内容. 在这里记录一种方法,用UITextField的代理来实现View的上移来解决这个问题. 首先设置 ...

  7. ios键盘弹起 body的高度拉长,页面底部空白问题。ios软键盘将页面抵到上面后,关闭软键盘页面不回弹的问题。

    js 监听ios手机键盘弹起和收起的事件 /* js 监听ios手机键盘弹起和收起的事件 */ document.body.addEventListener('focusin', () => { ...

  8. ios处理键盘的大小

    iOS的键盘有几个通知 UIKeyboardWillShowNotification UIKeyboardDidShowNotification UIKeyboardWillHideNotificat ...

  9. iOS数字键盘自定义按键

    UIKeyboardTypeNumberPad 数字键盘自定义按键 最近做一个搜索用户的功能,这里使用了UISearchBar.由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做: ...

随机推荐

  1. setOnLongClickListener中return值

    今天在做一个按钮的长按事件,长按的时候弹出一个Dialog弹出框,点击则是进入到下一个界面. 在我调试的时候,发现长按确实弹出了一个Dialog,但是同事他还跳转到下一个界面了. 这么说,就是在我长按 ...

  2. Android中的HTTP通信

    前言:近期在慕课网学习了慕课网课程Android中的HTTP通信,就自己总结了一下,其中参考了不少博文,感谢大家的分享. 文章内容包括:1.HTTP简介2.HTTP/1.0和HTTP/1.1之间的区别 ...

  3. 转载:WinForm中播放声音的三种方法

    转载:WinForm中播放声音的三种方法 金刚 winForm 播放声音 本文是转载的文章.原文出处:http://blog.csdn.net/jijunwu/article/details/4753 ...

  4. 使用xhprof分析php代码性能

    推荐在Linux平台使用xhprof,win下xhprof目前稳定版本在php5.5 安装xhprof 下载地址 http://pecl.php.net/get/xhprof-0.9.4.tgz 与p ...

  5. Java导入的项目乱码怎么解决?(Ⅱ)

    1.首先 打开  >>  Eclipse或Myeclipse.(我用的是Myeclipse) 2.打开  >>  Window  >>  Preferences  ...

  6. Ignite 配置更新Oracle JDBC Drive

           如果使用Oracle 12C 作为Ignite 的Repository的话,在Repository Createion Wizard的配置过程中,会出现ORA-28040:No matc ...

  7. SQLite学习笔记(六)&&共享缓存

    介绍 通常情况下,sqlite中每个连接都会一个独立的pager对象,pager对象中管理了该连接的缓存信息,通过pragma cache_size指令可以设置缓存大小,默认是2000个page,每个 ...

  8. 从零自学Hadoop(09):使用Maven构建Hadoop工程

    阅读目录 序 Maven 安装 构建 示例下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,Source ...

  9. OpenGL ES无法获取贴图数据原因

    最近在做一个项目,要从贴图中获取图像数据,查了很多资料,也琢磨很久,获取到的数据都是0.终于在一次偶然的机会,发现了端倪,成功了. 不得不说这"一分灵感"真的很重要 以下是在获取贴 ...

  10. openstack-swift云存储部署(二)

    接上篇,swift-proxy和swift-store的安装 先说一下服务器分配 swift-proxy和keystone部署在192.168.25.11 swift-store是两台  分别是192 ...