本文翻译自:Close iOS Keyboard by touching anywhere using Swift

I have been looking all over for this but I can't seem to find it. 我一直在寻找这个,但似乎找不到。 I know how to dismiss the keyboard using Objective-C but I have no idea how to do that using Swift ? 我知道如何使用Objective-C消除键盘,但我不知道如何使用Swift做到这一点? Does anyone know? 有人知道吗?


#1楼

参考:https://stackoom.com/question/1dERy/使用Swift触摸任意位置以关闭iOS键盘


#2楼

You can call 你可以打电话

resignFirstResponder()

on any instance of a UIResponder, such as a UITextField. 在UIResponder的任何实例(例如UITextField)上。 If you call it on the view that is currently causing the keyboard to be displayed then the keyboard will dismiss. 如果在当前导致键盘显示的视图上调用它,则键盘将关闭。


#3楼

Dash's answer is correct and preferred. 达世币的答案是正确的并且是首选。 A more "scorched earth" approach is to call view.endEditing(true) . 一种更“焦土”的方法是调用view.endEditing(true) This causes view and all its subviews to resignFirstResponder . 这将导致view及其所有子视图为resignFirstResponder If you don't have a reference to the view you'd like to dismiss, this is a hacky but effective solution. 如果您没有要关闭的视图的引用,那么这是一种骇人但有效的解决方案。

Note that personally I think you should have a reference to the view you'd like to have resign first responder. 请注意,我个人认为您应该参考要辞职的第一视图。 .endEditing(force: Bool) is a barbaric approach; .endEditing(force: Bool)是一种野蛮的方法; please don't use it. 请不要使用它。


#4楼

An answer to your question on how to dismiss the keyboard in Xcode 6.1 using Swift below: 有关以下有关如何使用Swift在Xcode 6.1中关闭键盘的问题的答案:

import UIKitclass ItemViewController: UIViewController, UITextFieldDelegate {@IBOutlet var textFieldItemName: UITextField!@IBOutlet var textFieldQt: UITextField!@IBOutlet var textFieldMoreInfo: UITextField!override func viewDidLoad() {super.viewDidLoad()textFieldItemName.delegate = selftextFieldQt.delegate = selftextFieldMoreInfo.delegate = self}.../*** Called when 'return' key pressed. return NO to ignore.*/func textFieldShouldReturn(textField: UITextField) -> Bool {textField.resignFirstResponder()return true}/*** Called when the user click on the view (outside the UITextField).*/override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {self.view.endEditing(true)}}

( Source of this information ). ( 此信息的来源 )。


#5楼

override func viewDidLoad() {super.viewDidLoad()//Looks for single or multiple taps. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")//Uncomment the line below if you want the tap not not interfere and cancel other interactions.//tap.cancelsTouchesInView = false view.addGestureRecognizer(tap)
}//Calls this function when the tap is recognized.
@objc func dismissKeyboard() {//Causes the view (or one of its embedded text fields) to resign the first responder status.view.endEditing(true)
}

Here is another way to do this task if you are going to use this functionality in multiple UIViewControllers : 如果要在多个UIViewControllers使用此功能,则这是另一种执行此任务的方法:

// Put this piece of code anywhere you like
extension UIViewController {func hideKeyboardWhenTappedAround() {let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))tap.cancelsTouchesInView = false            view.addGestureRecognizer(tap)}@objc func dismissKeyboard() {view.endEditing(true)}
}

Now in every UIViewController , all you have to do is call this function: 现在,在每个UIViewController ,您所需要做的就是调用此函数:

override func viewDidLoad() {super.viewDidLoad()self.hideKeyboardWhenTappedAround()
}

This function is included as a standard function in my repo which contains a lot of useful Swift Extensions like this one, check it out: https://github.com/goktugyil/EZSwiftExtensions 该函数作为标准函数包含在我的仓库中,其中包含许多类似这样的有用的Swift扩展,请查看: https : //github.com/goktugyil/EZSwiftExtensions


#6楼

You can also add a tap gesture recognizer to resign the keyboard. 您还可以添加轻击手势识别器以使键盘退出。 :D :D

override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.let recognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))backgroundView.addGestureRecognizer(recognizer)
}func handleTap(recognizer: UITapGestureRecognizer) {textField.resignFirstResponder()textFieldtwo.resignFirstResponder()textFieldthree.resignFirstResponder()println("tappped")
}

使用Swift触摸任意位置以关闭iOS键盘相关推荐

  1. iOS事件拦截(实现触摸任意位置隐藏指定view)

    项目里有一个需求,类似新浪或者腾讯微博的顶部title栏的类别选择器的消失(在选择器展开的时候,触摸屏幕任何地方使其消失). 最开始的想法是当这个选择器(selectorView)展开的时候,在当前屏 ...

  2. H5怎么关闭IOS键盘的首字母自动大写

  3. 如何关闭IOS键盘首字母自动大写

    解决方法: 添加属性 autocapitalize="off" <input type="text" autocapitalize="off&q ...

  4. linux系统取消虚拟键盘,关闭iOS虚拟键盘的六种方法

    第四种方法,通过轻击键盘之外的空白区域关闭虚拟键盘. 将屏幕上的view也就是textField的父视图拖一个touch down事件出来,和一个能关闭虚拟键盘的方法连接.如果视图没有touch do ...

  5. Swift - 触摸事件(点击,移动,抬起等)说明及用例

    在iOS开发中,UIGestureRecognizer可以方便的响应处理手势事件. 而如果要想更精细的处理,我们还需要借助touchesBegan,touchesMoved,touchesEnded等 ...

  6. RandomAccessFile 随机存取文件任意位置数据

    package _9RandomAccessFile类;import java.io.File; import java.io.FileNotFoundException; import java.i ...

  7. bmp图片显示(任意位置任意大小)

    文章目录 前言 图片显示 1.普通全屏(800*480)显示bmp 2.在任意位置显示任意大小bmp图片 头文件 前言 Linux系统,GEC6818,800*480,bmp图片 图片显示 由文件IO ...

  8. ios 位置服务器,关于 iOS 和 iPadOS 中的隐私与定位服务

    隐私设置 借助 iOS 和 iPadOS 中的隐私设置,您可以控制哪些 App 可以访问储存在您设备上的信息.例如,您可以允许社交网络 App 使用您的摄像头,以便拍摄照片并上传到该 App.您还可以 ...

  9. 【正版免费工具】 股票盯盘助手(简化版才154KB),无注册登录,可以悬浮任意桌面任意位置(包含任务栏)

    @[TOC]([正版免费工具] 股票盯盘助手(简化版才154KB),无注册登录,可以悬浮任意桌面任意位置(包含任务栏)) [正版免费工具] 股票盯盘助手(简化版才154KB),无注册登录,可以悬浮任意 ...

最新文章

  1. 激活函数之logistic sigmoid函数介绍及C++实现
  2. Myxls导出excel报表
  3. linux添加window启动
  4. MySQL笔记(九)查询结果添加序列号
  5. 【codevs1226】倒水问题,BFS练习
  6. python untill_Python telnetlib read_until返回切断字符串
  7. 京东金融创新”ABS云平台” 大数据提升直接融资效率
  8. JAVAWeb项目 微型商城项目-------(七)后台添加用户管理和商品类型管理操作
  9. centos7 里面dump_centos7使用lldb调试netcore应用转储dump文件
  10. C语言小程序打印楼梯图案
  11. 大气的酒店商务企业网站模板
  12. 前端实现打印页面的指定内容 + 快递面单的模板
  13. SCAU高级语言程序设计--实验5 循环结构(一)(2)
  14. 也许你需要看看这篇文章
  15. 2016 server win 假死_Windows Server下Apache假死堵塞卡死无响应解决方法
  16. 1.1 win10下wget的安装
  17. 【ElasticSearch从入门到放弃系列 零】ElasticSearch看这一篇就够了
  18. 防止恶意刷新页面的Java实现
  19. 用论文写作平台Overleaf写中文论文
  20. 拒绝洗脑式广告,无限重复洗脑式广告应该受到谴责。怎么有效抵制这类广告?...

热门文章

  1. 驱动,包括很多软件,并不是最新的就是最好的
  2. 步长条件梯度下降算法步长和收敛条件的设置的一些看法
  3. JDialog简单使用
  4. bosboot -a的作用
  5. react+wabpack 搭建
  6. Web.config配置详解
  7. ios Class类型使用
  8. 在db2中 两个数据库之间的两个表的联合查询
  9. 简明Python3教程 15.异常
  10. Section 1.3 milk