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


当前回答

我没有看到它被提到过,但是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)
}

其他回答

另一种可能是在你可能需要触摸的所有视图下面添加一个没有内容的大按钮。 给它一个名为:

@IBAction func dismissKeyboardButton(sender: AnyObject) {
    view.endEditing(true)
}

对我来说,手势识别器的问题是,它也捕捉了我想通过tableViewCells接收的所有触摸。

斯威夫特3

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

因为我编辑的@King-Wizard的回答被拒绝了,所以发布了一个新的答案。

让你的类成为UITextField的委托并重写touchesBegan。

斯威夫特4

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        textField.delegate = self
    }

    //Called when 'return' key is pressed. Return false to keep the keyboard visible.
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        return true
    }

    // Called when the user clicks on the view (outside of UITextField).
    override func touchesBegan(touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

}

斯威夫特3: 关闭键盘的最简单方法:

  //Dismiss keyboard method
    func keyboardDismiss() {
        textField.resignFirstResponder()
    }

    //ADD Gesture Recignizer to Dismiss keyboard then view tapped
    @IBAction func viewTapped(_ sender: AnyObject) {
        keyboardDismiss()
    }

    //Dismiss keyboard using Return Key (Done) Button
    //Do not forgot to add protocol UITextFieldDelegate 
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        keyboardDismiss()

        return true
    }

您还可以添加一个点击手势识别器来重新使用键盘。: 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")
}