方法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. iOS多线程之4.GCD简介

    GCD(Grand Central Dispatch)应该是我们开发中最常用到的多线程解决方案,是苹果公司专门为多核的并行运算提出的解决方案,是基于C语言的,提供了很多非常强大的函数. GCD的优势 ...

  2. Android4.4访问外部存储

    在Android 4.4系统中,外置存储卡(SD卡)被称为二级外部存储设备(secondary storage),应用程序已无法往外置存储卡(SD卡)写入数据,并且WRITE_EXTERNAL_STO ...

  3. 把自己Github上的代码添加Cocoapods支持

    转载请注明原链接:http://www.cnblogs.com/zhanggui/p/6003481.html 一.前言 这两天被cocoapods折磨的心力憔悴.看cocoapods官网的添加支持, ...

  4. FMDB第三方框架

    FMDB是同AFN,SDWebImage同样好用的第三方框架,它以OC的方式封装了SQLite的C语言API,使得开发变得简单方便. 附上github链接https://github.com/ccgu ...

  5. OC #import和@class的用法和区别

    OC #import和@class的用法和区别 import会包含这个类的所有信息,包括实体变量和方法,而@class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,暂时不用考虑, ...

  6. 干货发布:VSS文件清理工具

    一.功能:1.删除VSS文件(以.vssscc,.scc,.vspscc为后缀的文件)2.去除文件解决方案文件和C#项目文件中的VSS标签3.删除Bin和Obj目录 二.开发工具:vs 2010 三. ...

  7. HDFS --访问

    Hdfs的访问方式有两种,第一:类似linux命令,hadoop shell.第二:java API方式. 先看第一种. FS Shell cat chgrp chmod chown copyFrom ...

  8. Java多线程-并发容器

    Java多线程-并发容器 在Java1.5之后,通过几个并发容器类来改进同步容器类,同步容器类是通过将容器的状态串行访问,从而实现它们的线程安全的,这样做会消弱了并发性,当多个线程并发的竞争容器锁的时 ...

  9. Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理1

    首先给上项目的整体框架图:,这里我没有使用BLL,因为感觉太烦了就没有去使用. 那么接下来我们首先先去Model层中添加Model. 管理员类: using System; using System. ...

  10. Oracle Listener 动态注册 与 静态注册

    http://blog.csdn.net/tianlesoftware/article/details/5543166