我一直在到处找这个,但我似乎找不到。我知道如何解雇使用Objective-C键盘,但我不知道如何使用Swift?有人知道吗?


当前回答

可以通过在AppDelegate的窗口属性中添加全局点击手势识别器来实现这一点。

这是一种非常全面的方法,对某些人来说可能不是理想的解决方案,但对我来说很有效。请让我知道这个解决方案是否有任何陷阱。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Globally dismiss the keyboard when the "background" is tapped.
        window?.addGestureRecognizer(
            UITapGestureRecognizer(
              target: window, 
              action: #selector(UIWindow.endEditing(_:))
            )
        )

        return 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

我发现最好的解决方案包括来自@Esqarrouth的接受答案,并进行了一些调整:

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboardView")
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

    func dismissKeyboardView() {
        view.endEditing(true)
    }
}

电话响了。cancelsTouchesInView = false是关键的:它确保UITapGestureRecognizer不会阻止视图上的其他元素接收用户交互。

方法解雇键盘()被更改为稍微不那么优雅的解雇keyboardview()。这是因为在我的项目相当旧的代码库中,有很多次已经使用了dismissKeyboard()(我想这并不少见),导致编译器问题。

然后,如上所述,这个行为可以在各个视图控制器中启用:

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

一个简单的方法是选择文本字段并使用方法enditing (true)

e.g

exampleTextField.endEditing(true)

Swift 5只要两行就够了。添加到你的viewDidLoad应该工作。

 let tapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing))
 view.addGestureRecognizer(tapGesture)

如果你的点击手势阻止了一些其他的触摸,那么添加这一行:

tapGesture.cancelsTouchesInView = false

以下是如何通过点击其他地方来消除键盘,使用Swift 5只需两行。

(我讨厌添加另一个答案,但由于这是谷歌上的顶级结果,我将帮助像我这样的新手。)

在你的ViewController.swift中,找到viewDidLoad()函数。

加上这两行:

let tap: UIGestureRecognizer = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing))
        
view.addGestureRecognizer(tap)