方法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开发-UI 从入门到精通(四)

    一.UITextField 1.UITextField是什么? (1)UITextField(输入框):是控制文本输入和显示的控件.在App中UITextField出现频率也比较高: (2)iOS系统 ...

  2. OpenGL中glVertex、显示列表(glCallList)、顶点数组(Vertex array)、VBO及VAO区别

    OpenGL中glVertex.显示列表(glCallList).顶点数组(Vertex array).VBO及VAO区别 1.glVertex 最原始的设置顶点方法,在glBegin和glEnd之间 ...

  3. Android添加图片到ListView或者 RecyclerView显示

    先上图 点击+号就去选择图片 实际上这个添加本身就是一个ListView或者 RecyclerView 只是布局有些特殊 item <?xml version="1.0" e ...

  4. iOS 学习 - 18.TextField 自定义菜单事件,复制和微信分享

    菜单事件包括,剪切.拷贝.全选.分享...,此 demo 只有 copy.share 1.定义 field 继承与 UITextField - (BOOL)canPerformAction:(SEL) ...

  5. PHP开发之Zend Studio快捷键汇总

    应用场景 快捷键 功能 查看快捷键 ctrl+shift+l 显示所有快捷键列表 查看和修改快捷键 打开Window->Preferences->General->keys 修改字体 ...

  6. LVS+Keepalived+Squid+Nginx+MySQL主从高性能集群架构部署方案

    方案一,在tomcat的workers.properties里面配置相关条件 worker.tomcat.lbfactor= worker.tomcat.cachesize= worker.tomca ...

  7. 实时事件统计项目:优化solr和morphline的时间字段

    morphline优化,如下: 传过来的时间戳被复制到3个字段:eventTimeInMinuteChina_tdt ,eventTimeInMinuteUTC_tdt ,eventTimeInHou ...

  8. SQLSERVER中正则表达式封装使用

    封装好的正则表达式供SQLSERVER调用 打开数据库->可编程性->函数->标量值函数->新建标量值函数名 USE [数据库]GOSET ANSI_NULLS ONGOSET ...

  9. linux top 源码分析

    /* * Copyright (c) 2008, The Android Open Source Project * All rights reserved. * * Redistribution a ...

  10. Tomcat 启动花费很长时间的解决方案

    原始解决方案链接 将 $JAVA_PATH/jre/lib/security/java.security 中的 securerandom.source=file:/dev/urandom 替换为 se ...