总结了以下几种方式,欢迎补充

 1,为空白区域绑定Touch Up Inside事件

 2,重写touchesEnded方法

 3,为TextField绑定Did End On Exit事件

1,点击编辑区域以外的地方时关闭(空白处区域绑定Touch Up Inside事件

    新建一个项目,打开Main.storyboard,添加一个Text Field,与ViewController建立连接,然后点击空白处,在右边窗口修改Custom Class 的class改为UIControl

     

   然后为UIControl绑定Touch Up Inside事件(只有改为UIControl后才能绑定事件)

   

  ViewController:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var name: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} @IBAction func CloseKeyBoard(sender: AnyObject) {
name.resignFirstResponder();
} }

2,点击编辑区域以外的地方时关闭(重写touchesEnded)

        只需要在ViewController里重写touchesEnded方法

//触摸事件,当一个或多个手指离开屏幕时触发
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
name.resignFirstResponder();
}

  

3,点击软键盘右下角的Done/Go/Next...关闭键盘(为TextField绑定Did End On Exit事件)

   选择Main.storyboard中的Text Field,按住control拖拉的方式为其绑定Did End On Exit事件     

   

 @IBAction func DoneCloseKeyBoard(sender: AnyObject) {
name.resignFirstResponder();
}

  

最新文章

  1. DEV word文档转换为pdf文件
  2. jquery中attr和prop的区别、 什么时候用 attr 什么时候用 prop (转自 芈老头 )
  3. 基本药目录sop
  4. python学习笔记三 函数(基础篇)
  5. 10个很有用的高级Git命令
  6. 用JavaScript(js)对时间格式化
  7. CoffeeScript NgComponent
  8. linux下利用curl监控web应用状态
  9. BinarySearchTree-二叉搜索树
  10. java排序算法(八):希尔排序(shell排序)
  11. 深入解析ThreadLocal 详解、实现原理、使用场景方法以及内存泄漏防范 多线程中篇(十七)
  12. 30分钟了解Springboot整合Shiro
  13. Fiddler忽略捕捉大文件流
  14. NOIP2016 巨凉无比的感言
  15. 自定义安装visual studio 2010开发asp.net
  16. 【May Be DNK】JSON.parse() and JSON.stringify()的两个实用技巧
  17. ASP.NET Core 与 .NET Core 演变与基础概述
  18. MAC安装最新datagrip之后无法非官方激活,而且启动过慢
  19. Azure ServiceBus的消息中带有@strin3http//schemas.microsoft.com/2003/10/Serialization/�
  20. 初涉RxAndroid结合Glide实现多图片载入操作

热门文章

  1. 20162326 齐力锋 2017-2018学期 Bag类的补写博客
  2. 2018-2019-2 20165114《网络对抗技术》Exp4 恶意代码分析
  3. Cisco、HUAWEI、H3c、Firewall等设备配置snmp
  4. 详细解读ARM寄存器之CPSR【转】
  5. [nowcoder]再编号
  6. 对拷 使用scp命令在两台linux上对拷文件或者文件夹
  7. VMware Workstation 12 增加磁盘容量 Windows Server 2012 系统 扩展
  8. windows系统下载地址大全&amp;大白菜下载和教程
  9. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
  10. tsunami:一种基于UDP协议的快速传输