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.func dismissKeyboard() { //Causes the vIEw (or one of its embedded text fIElds) to resign the first responder status. vIEw.endEditing(true)}
================================================== ======================
编辑:如果要在多个UIVIEwControllers中使用此功能,这里是另一种方法来执行此任务:
// Put this pIEce of code anywhere you likeextension UIVIEwController { func hIDeKeyboarDWhenTappedAround() { let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(UIVIEwController.dismissKeyboard)) vIEw.addGestureRecognizer(tap) } func dismissKeyboard() { vIEw.endEditing(true) }}
现在在每个UIVIEwController,所有你要做的是调用此函数:
overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() self.hIDeKeyboarDWhenTappedAround() }
这个函数作为一个标准函数包含在我的仓库,其中包含了很多有用的Swift扩展,像这样,检查:https://github.com/goktugyil/EZSwiftExtensions
总结以上是内存溢出为你收集整理的使用Swift在任何位置触摸可关闭iOS键盘全部内容,希望文章能够帮你解决使用Swift在任何位置触摸可关闭iOS键盘所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)