第四种方法,通过轻击键盘之外的空白区域关闭虚拟键盘。

将屏幕上的view也就是textField的父视图拖一个touch down事件出来,和一个能关闭虚拟键盘的方法连接。如果视图没有touch down事件,可将view的父类从UIView修改为UIButton。

首先定义并实现一个方法backgroundTap:。

- (IBAction) backgroundTap:(id)sender

{

NSArray *subviews = [self.view subviews];

for (id objInput in subviews) {

if ([objInput isKindOfClass:[UITextField class]]) {

UITextField *theTextField = objInput;

if ([objInput isFirstResponder]) {

[theTextField resignFirstResponder];

}

}

}

}

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

第五种方法,使用endEditing:方法

在所在的视图控制器类中,覆盖这个方法。

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

[[self view] endEditing:YES];

}

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]方法。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

UIView *result = [super hitTest:point withEvent:event];

[self endEditing:YES]

return result;

}

我将视图控制器的主视图所属类修改为这个新建视图类。这样在屏幕上轻击任何位置都会关闭虚拟键盘。

这个方法是最简单,也是最好的关闭虚拟键盘的方法。

使用好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.

linux系统取消虚拟键盘,关闭iOS虚拟键盘的六种方法相关推荐

  1. 虚拟机Linux系统忘记密码修改root或其他用户密码的方法

    这篇文章主要介绍了虚拟机Linux系统忘记密码修改root或其他用户密码的方法. 注意事项:本文基于centos7环境进行操作,由于centos的版本是有差异的,继续之前请确定好版本. 一.重启系统, ...

  2. linux不能强制显卡分辨率,Linux系统装显卡驱动及分辨率不正常的解决方法

    Linux系统装显卡驱动及分辨率不正常的解决方法 发布时间:2014-06-13 16:09:53来源:红联作者:velcbo 驱动安装: 这里得说明一下,安装新的显卡驱动也有不完美的地方,就是开机进 ...

  3. linux修改root密码bad,虚拟机Linux系统忘记密码修改root或其他用户密码的方法

    使用场景 linux管理员忘记root密码,需要进行找回操作. 注意事项:本文基于centos7环境进行操作,由于centos的版本是有差异的,继续之前请确定好版本. 步骤 一.重启系统,在开机过程中 ...

  4. php获取当前设备,Linux_在Linux系统中使用lsblk和blkid显示设备信息的方法,今天我们将会向你展示如何使 - phpStudy...

    在Linux系统中使用lsblk和blkid显示设备信息的方法 今天我们将会向你展示如何使用 lsblk 和 blkid 工具来查找关于块设备的信息,我们使用的是一台安装了 CentOS 7.0 的机 ...

  5. 在Linux系统的命令行中为MySQL创建用户的方法

    这篇文章主要介绍了在Linux系统的命令行中为MySQL创建用户的方法,包括对所建用户的权限管理,需要的朋友可以参考下 要访问一个MySQL服务器,你需要使用一个用户帐号登录其中方可进行.每个MySQ ...

  6. linux系统Nginx下载安装步骤(含报错解决方法)

    linux系统Nginx下载安装步骤(含报错解决方法) 基础步骤 安装过程可能出现的报错及解决方法 执行 ./configure --prefix=/opt/nginx 报错 执行 make inst ...

  7. Linux系统时间比现在时间快8小时的解决方法

    Linux系统时间比现在时间快8小时的解决方法: 1.vi /etc/sysconfig/clock #编辑文件 ZONE="Asia/Shanghai" UTC=false #设 ...

  8. winscp 登录 am4379 的 linux系统 连接被意外关闭

    winscp 登录 am4379 的 linux系统,提示 列出'/home/root'的目录项时出错,连接被意外关闭 具体提示:对话框提示,列出'/home/root'的目录项时出错. 连接被意外关 ...

  9. linux系统取消时间同步,Linux系统时间不同步问题

    问题 : data命令查看系统时间与实际实际一致,但日志中的实际却与实际时间差了整整12个小时,可能原因是什么?如何处理.原因和解决办法? //查看时间 [root@localhost ~]# dat ...

最新文章

  1. yarn client 提交任务
  2. jquery对radio的操作汇总
  3. 信捷电子凸轮使用_FM352电子凸轮使用指南
  4. oracle sql序列,SQL server 和Oracle 序列
  5. easyui-combobox
  6. frida hook java 函数_使用 Frida 来 Hook Java 类中的构造函数(构造函数带重载),获取解密后的js脚本...
  7. LVS/NAT的配置和应用
  8. IE10横空出世,一统江湖
  9. 大数据发展的根基是什么?
  10. 基于Java的OA系统的设计与实现
  11. Mybatis查询之list作为参数查询
  12. 以衍复为例,聊聊当下的沪深300指数增强
  13. ChinaSoft 论坛巡礼 | 云际计算系统软件
  14. 世界上最伟大的十大公式
  15. 智慧交通怎样利用科技打造一个“最强大脑”
  16. android 给图片加文字、图片水印
  17. 信息搜集:网络空间搜索引擎(Shodan)语法及API应用案例
  18. 中关村e谷为产业搭台:中关村论坛(空天专场)黑科技亮相现场
  19. 华为ensp搭建习题
  20. PyCryptodome

热门文章

  1. 【108期分享】4款产品发布PPT模板免费下载
  2. 群晖安装docker utorrent简明教程-傻子看了都会
  3. C#判断字符串数组中是否有重复项
  4. 自动化体系平台建设 —— 全流程质量保证及改进措施
  5. c语言编写消防车声音程序教程,单片机实现消防车报警声音的设计
  6. java毕业设计——基于java+TCP+UDP的局域网聊天室系统设计与实现(毕业论文+程序源码)——局域网聊天室系统
  7. 深圳数据分析岗需求分析
  8. matlab gui电机,基于MATLAB GUI的感应电机性能分析界面设计.docx
  9. 植物大战僵尸原版--简单修改攻略
  10. 对计科核心课程的关系的总结