在iOS应用开发中,有三类视图对象会打开虚拟键盘,进行输入操作,但如何关闭虚拟键盘,却没有提供自动化的方法。这个需要我们自己去实现。这三类视图对象分别是UITextField,UITextView和UISearchBar。
这里介绍一下UITextField中关闭虚拟键盘的几种方法。

(miki西游 @mikixiyou 原文链接: http://mikixiyou.iteye.com/blog/1753330 )

第一种方法,使用它的委托UITextFieldDelegate中的方法textFieldShouldReturn:来关闭虚拟键盘。
在UITextField视图对象如birdNameInput所在的类中实现这个方法。

C代码  
  1. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  2. if ((textField == self.birdNameInput) || (textField == self.locationInput)) {
  3. [textField resignFirstResponder];
  4. }
  5. return YES;
  6. }

这样,在输入框birdNameInput中打开虚拟键盘后,轻击键盘的return键就会自动关闭掉虚拟键盘。

第二种方法,将birdNameInput的属性中Return Key修改为done,再定义一个方法和Done键的Did End On Exit连接。通过轻击done键触发这个事件来关闭虚拟键盘。
定义的方法如下:

C代码  
  1. - (IBAction) textFieldDoneEditing:(id)sender
  2. {
  3. [sender resignFirstResponder];
  4. }

这两个方法都是轻击虚拟键盘上一个键来关闭它。这属于精确操作,而手指不像鼠标,做这种操作不容易。因此就UI层面而言,这两个方法都不是最好的方法。
在iphone或ipad屏幕上,虚拟键盘占用的面积大小是有限的。通过轻击虚拟键盘之外的区域而关闭虚拟键盘。

第三种方法,通过轻击键盘之外的空白区域关闭虚拟键盘。
在birdNameInput所属的视图控制器类的viewDidLoad方法中定义一个UITapGestureRecognizer的对象,然后将它赋值为它的视图。

C代码  
  1. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]   initWithTarget:self action:@selector(dismissKeyboard)];
  2. [self.view addGestureRecognizer:tap];
  3. [tap release];

再定义一下选择器调用的方法dismissKeyboard。

C代码  
  1. -(void)dismissKeyboard {
  2. [birdNameInput resignFirstResponder];
  3. }

如果屏幕上有多个textField的话,一个一个地列出来就有些麻烦。那么将方法修改一下,如下:

C代码  
  1. -(void)dismissKeyboard {
  2. NSArray *subviews = [self.view subviews];
  3. for (id objInput in subviews) {
  4. if ([objInput isKindOfClass:[UITextField class]]) {
  5. UITextField *theTextField = objInput;
  6. if ([objInput isFirstResponder]) {
  7. [theTextField resignFirstResponder];
  8. }
  9. }
  10. }
  11. }

如果这个屏幕上的视图对象很复杂的话,另当别论。
这个方法是编码新建一个手势对象。也可以直接使用interface builder图形化开发工具,在storyboard中拉入一个手势对象到视图控制器类中,再将此手势对象建立一个IBACTION,名称可以是dismissKeyboard。

第四种方法,通过轻击键盘之外的空白区域关闭虚拟键盘。
将屏幕上的view也就是textField的父视图拖一个touch down事件出来,和一个能关闭虚拟键盘的方法连接。如果视图没有touch down事件,可将view的父类从UIView修改为UIButton。
首先定义并实现一个方法backgroundTap:。

C代码  
  1. - (IBAction) backgroundTap:(id)sender
  2. {
  3. NSArray *subviews = [self.view subviews];
  4. for (id objInput in subviews) {
  5. if ([objInput isKindOfClass:[UITextField class]]) {
  6. UITextField *theTextField = objInput;
  7. if ([objInput isFirstResponder]) {
  8. [theTextField resignFirstResponder];
  9. }
  10. }
  11. }
  12. }

然后选择背景视图的Touch Down事件,连接 backgroundTap:即可。这样只要轻击一下虚拟键盘之外的区域,就能关闭虚拟键盘。这些方法都是使用resignFirstResponder方法来关闭虚拟键盘,还有其他的方法。

第五种方法,使用endEditing:方法
在所在的视图控制器类中,覆盖这个方法。

C代码  
  1. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  2. [[self view] endEditing:YES];
  3. }

This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
但是,如果这个屏幕很复杂,虚拟键盘之外的区域中有很多按钮。轻击这些区域时可能会轻击到这些按钮,这样虚拟键盘就不能关闭。
要是找到一个没有按钮的空白区域都不容易且还有隐藏的视图对象时,通过轻击虚拟键盘之外的区域关闭虚拟键盘的方法实现起来就难了。

第六种方法,覆盖hitTest:withEvent:方法关闭虚拟键盘

在stackoverflow.com上,有人这样总结。说使用hitTest:withEvent:方法是最好的,也是最容易的解决方法。

I think the easiest (and best) way to do this is to subclass your global view and use hitTest:withEvent method to listen to any touch. 
Touches on keyboard aren't registered, so hitTest:withEvent is only called when you touch/scroll/swipe/pinch... somewhere else, then call [self endEditing:YES].
This is better than using touchesBegan because touchesBegan are not called if you click on a button on top of the view. 
It is better than UITapGestureRecognizer which can't recognize a scrolling gesture for example. It is also better than using a dim screen because in a complexe and dynamic user interface, you can't put dim screen every where. Moreover, it doesn't block other actions, you don't need to tap twice to select a button outside (like in the case of a UIPopover).
Also, it's better than calling [textField resignFirstResponder], because you may have many text fields on screen, so this works for all of them.

因此,我再建立一个继承UIView的视图类。在这个视图类中,覆盖hitTest:withEvent:方法,增加[self endEditing:YES]方法。

C代码  
  1. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  2. UIView *result = [super hitTest:point withEvent:event];
  3. [self endEditing:YES]
  4. return result;
  5. }

我将视图控制器的主视图所属类修改为这个新建视图类。这样在屏幕上轻击任何位置都会关闭虚拟键盘。
这个方法是最简单,也是最好的关闭虚拟键盘的方法。
使用好hitTest:withEvent:这个方法,还可以实现很多很复杂的功能。
The implementation of hitTest:withEvent: in UIResponder does the following:

  • It calls pointInside:withEvent: of self
  • If the return is NO, hitTest:withEvent: returns nil. the end of the story.
  • If the return is YES, it sends hitTest:withEvent: messages to its subviews. it starts from the top-level subview, and continues to other views until a subview returns a non-nil object, or all subviews receive the message.
  • If a subview returns a non-nil object in the first time, the first hitTest:withEvent: returns that object. the end of the story.
  • If no subview returns a non-nil object, the first hitTest:withEvent: returns self

This process repeats recursively, so normally the leaf view of the view hierarchy is returned eventually.
However, you might override hitTest:withEvent to do something differently. In many cases, overriding pointInside:withEvent: is simpler and still provides enough options to tweak event handling in your application.

转载地址:http://mikixiyou.iteye.com/blog/1753330

IOS 收起键盘的几种方法(转)相关推荐

  1. IOS收起键盘的几种办法(摘抄自唐巧《iOS开发进阶》)

    在UIViewController中收起键盘,除了调用相应控件的resignFirstResponder方法外,还有另外3种方法: 1.重载UIViewController中的touchesBegin ...

  2. IOS 点击空白处隐藏键盘的几种方法

    IOS7 点击空白处隐藏键盘的几种方法 IOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我们可以 ...

  3. 请把ios文件解压出来是什么意思_手机资讯:最新屏蔽 iOS升级弹窗的 2 种方法

    如今使用IT数码设备的小伙伴们是越来越多了,那么IT数码设备当中是有很多知识的,这些知识很多小伙伴一般都是不知道的,就好比最近就有很多小伙伴们想要知道最新屏蔽 iOS升级弹窗的 2 种方法,那么既然现 ...

  4. iOS中收起键盘的几种方式

    在使用UITextField的时候,我们会和键盘打交道,有时候要求弹出来的键盘类型是数字键盘,有时候要求我们弹出来的是一般的键盘.当我们输入完成的时候,就涉及到在上什么时候收回键盘的事情了.收回键盘分 ...

  5. linux键盘模拟程序,linux下模拟键盘的几种方法

    1.使用GTK中的GdkEvent GdkEvent *event; event = gdk_event_new (GDK_KEY_PRESS);              //按键按下 event- ...

  6. android ios动态模糊效果,iOS实现模糊效果的几种方法

    iOS系统中大量使用了模糊效果,背景模糊,使得靠前的内容更容易获得用户的关注,自己开发中肯定也遇到过样的需求.今天就总结一下常用的实现模糊效果的方法,目录如下图: 本篇demo:iOS常用模糊效果de ...

  7. iOS 字体适配的几种方法总结

    在iOS开发中,有些公司对字体也有适配要求,为了让字体美观,所以在不同尺寸的屏幕上字体大小也要做到适配. 自己总结了几种方法供大家参考. 方法一:用宏定义适配字体大小(根据屏幕尺寸判断) //宏定义 ...

  8. iOS收起键盘的常用方法

    iOS没有收起键盘的按钮,一般的应用都是让用户点击屏幕其它位置来收起键盘,或者点击return键时收起键盘. import UIKitclass LoginViewController: UIView ...

  9. iOS拨打电话的三种方法

    iOS里面在程序里面实现拨打电话的方式,略知以下三种方法: ps:其实仔细看起来没什么特殊的,但要特殊注意代码里面的关键词(下面用红色标出以作区别) 1.此方法,拨打完电话但是回不到原来的应用,会停留 ...

最新文章

  1. Distance metric learning
  2. python 网页登录selenium_使用selenium登录网页
  3. Android7.1的EDP屏替换
  4. 还在用 Win?教你从零把 Mac 打造成开发利器
  5. bootstrap 输入错误提示_win7系统提示explorer.exe应用程序错误怎么办
  6. 腾讯的一道链表笔试题【总结】
  7. Linux Redis自动启动,Redis开机启动,Linux Redis设置开机启动
  8. 好男人是这样爱老婆的
  9. 多线程面试体系列(13):多线程同步内功心法——PV操作下
  10. java.lang.IllegalArgumentException: Must specify o
  11. 蓝桥杯 ALGO-140 算法训练 P1101
  12. 2015年01月30日 - Git五分钟教程
  13. 虹科教您固定工业树莓派Modbus RTU设备编号
  14. Chrome浏览器打开Axure原型图
  15. 欧拉-拉格朗日方程(The Euler-Lagrange equation)
  16. LDN双模键盘常见问题(FAQ)
  17. day10、2 - 小小综合实验升级
  18. PhotoShop如何使用图层之实例演示?
  19. NodeJS 运行环境
  20. Java从输入中读取一个数组

热门文章

  1. 直线制动优化设计MATLAB程序
  2. 消息队列(定义、结构、如何创建、消息队列的发送与接收、发送与接收实例)
  3. oracle组合索引失效_oracle 索引失效原因
  4. #二、江恩角度线的使用问题(一)、江恩角度线的错误用法
  5. 2021杭电多校补题(5)
  6. [附源码]java毕业设计基于SSM高考志愿填报系统
  7. Python Pandas 常用的数据结构有哪些?详解Series、DataFrame、Index数据结构。
  8. 使用RAMDisk制作内存盘加快系统运行速度(并解决部分问题)
  9. Java.IO的概述
  10. Laconic:从JavaScript生成DOM内容的新方法