我一直在到处找这个,但我似乎找不到。我知道如何解雇使用Objective-C键盘,但我不知道如何使用Swift?有人知道吗?
当前回答
override func viewDidLoad() {
super.viewDidLoad()
//Looks for single or multiple taps.
let tap = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.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)
}
如果你打算在多个uiviewcontroller中使用这个功能,这里有另一种方法来完成这个任务:
// Put this piece of code anywhere you like
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
现在在每个UIViewController中,你要做的就是调用这个函数:
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
这个函数是我的repo中包含的标准函数,它包含了很多有用的Swift扩展,比如这个,看看:https://github.com/goktugyil/EZSwiftExtensions
其他回答
在Swift 4中,添加@objc:
在viewDidLoad中:
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
view.addGestureRecognizer(tap)
功能:
@objc func dismissKeyboard() {
view.endEditing(true)
}
我没有看到它被提到过,但是NotificationCenter可以告诉你键盘何时打开和关闭。例如,如果你想改变cancelsTouchesInView或一起删除手势识别器,这可以被使用。
例子:
var dismissKeyboardGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
//Setup dismiss keyboard gesture
self.dismissKeyboardGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
//Add Notifications
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name:UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide), name:UIResponder.keyboardDidHideNotification, object: nil)
}
@objc func keyboardDidShow() {
dismissKeyboardGestureRecognizer.cancelsTouchesInView = true
view.addGestureRecognizer(dismissKeyboardGestureRecognizer)
}
@objc func keyboardDidHide() {
dismissKeyboardGestureRecognizer.cancelsTouchesInView = false
view.removeGestureRecognizer(dismissKeyboardGestureRecognizer)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
//Looks for single or multiple taps.
let tap = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.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)
}
如果你打算在多个uiviewcontroller中使用这个功能,这里有另一种方法来完成这个任务:
// Put this piece of code anywhere you like
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
现在在每个UIViewController中,你要做的就是调用这个函数:
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
这个函数是我的repo中包含的标准函数,它包含了很多有用的Swift扩展,比如这个,看看:https://github.com/goktugyil/EZSwiftExtensions
添加这个扩展到你的ViewController:
extension UIViewController {
// Ends editing view when touches to view
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.view.endEditing(true)
}
}
我更喜欢这样一句话:
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "dismissKeyboardFromView:"))
只要把它放在override viewDidLoad函数中,不管你想让它发生在哪个子类UIViewController中,然后把下面的代码放在你的项目中的一个新的空文件中,名为“UIViewController+dismissKeyboard.swift”:
import UIKit
extension UIViewController {
// This function is called when the tap is recognized
func dismissKeyboardFromView(sender: UITapGestureRecognizer?) {
let view = sender?.view
view?.endEditing(true)
}
}
推荐文章
- 如何在iOS上进行base64编码?
- 如何停止不必要的UIButton动画标题变化?
- 是否有可能更新一个本地化的故事板的字符串?
- 以编程方式获取Bundle Identifier
- 为iPad和iPhone设计输入按钮
- 如何在我的iPhone应用程序中使用NSError ?
- 我的预发行应用已经在iTunes Connect中“处理”了一周多,这是怎么回事?
- Xcode iOS项目只显示“我的Mac 64位”,但不显示模拟器或设备
- Objective-C中的自动引用计数不能防止或减少什么样的泄漏?
- 在Xcode 9中如何从打开的多个模拟器中退出或关闭单个模拟器?
- 如何使用Swift播放声音?
- UINavigationBar自定义返回按钮没有标题
- 如何精确地以毫秒为单位记录方法的执行时间?
- 在整个UIWindow中获取UIView的位置
- 如何解散ViewController在Swift?