在iOS中关闭键盘

时间:2017-11-14 18:55:26

标签: ios swift keyboard

我查看并尝试了Swift 3,Xcode 8的多种解决方案,但无法使用。我试过了:

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

并将文本字段输入设置为第一响应者:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {            
    pressureInput.resignFirstResponder()
}

我不知道从Xcode 8到Xcode 9的某些东西导致这些方法不起作用,或者我是否在其他地方搞砸了。我有9个文本字段,他们都设置了委托给自己。它们的标签会增加,以便在按下返回时移动到下一个文本字段。不要以为这会影响它。对不起,新来的!这些尝试函数中的任何一个都可以正常运行代码,但键盘保持不变。我想在任何文本字段之外触摸时解除键盘。

5 个答案:

答案 0 :(得分:10)

首先在任何swift文件中编写此扩展名

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)

    }

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

在viewDidLoad中,只有在任何视图控制器中调用View时才有textFields。

self.hideKeyboardWhenTappedAround()

答案 1 :(得分:0)

您确定正在调用touchesBegan吗?如果您确定,请尝试将self.resignFirstResponder()添加到touchesBegan功能中。这告诉您的视图控制器它不再是第一个响应者,应该关闭键盘。

如果没有,您要做的就是创建UITapGestureRecognizer,将其添加到您的视图中,然后将其连接到调用self.resignFirstResponder()的函数。

答案 2 :(得分:0)

您是否尝试调试程序以查看代码是否在函数中停止(具有断点)?通常此代码应该有效...检查 textFields 是否在超级视图子视图中,如果它们可能是您应该致电self.childView.endEditing(true)

如果您真的使用多个textFields,可能应该尝试 IQKeyboardManager 库。我在我的所有项目中都使用它。你可以在这里找到它:https://github.com/hackiftekhar/IQKeyboardManager。使用简单,支持性好。只需通过可可豆荚安装它,将IQKeyboardManager.sharedManager().enable = true放入AppDelegate即可,您已准备就绪。 :)

答案 3 :(得分:0)

快捷键4、5。在点击并返回按钮时,我总是使用隐藏键盘。

override func viewDidLoad() {
    super.viewDidLoad()
    hideKeyboardWhenTappedAround()
    emailTextField.delegate = self // your UITextfield
    passwordTextField.delegate = self // your UITextfield
}

//隐藏键盘

extension EmailAutorization: UITextFieldDelegate {

 // Return button tapped
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

// Around tapped
func hideKeyboardWhenTappedAround() {
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(EmailAutorization.dismissKeyboard))
    tap.cancelsTouchesInView = false
    view.addGestureRecognizer(tap)
}

@objc func dismissKeyboard() {
    view.endEditing(true)
}

}

答案 4 :(得分:0)

这是关闭键盘并在键盘打开时向上移动视图的解决方案:Swift 5

   override func viewDidLoad() {
    super.viewDidLoad()
    let tap = UITapGestureRecognizer(target: self, action: #selector(taped))
    view.addGestureRecognizer(tap)
    NotificationCenter.default.addObserver(self, selector: #selector(KeyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(KeyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}

 //This Method Will Hide The Keyboard
@objc func taped(){
  self.view.endEditing(true)   
}

@objc func KeyboardWillShow(sender: NSNotification){

    let keyboardSize : CGSize = ((sender.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size)!
    if self.view.frame.origin.y == 0{
        self.view.frame.origin.y -= keyboardSize.height
    }

}

@objc func KeyboardWillHide(sender : NSNotification){

    let keyboardSize : CGSize = ((sender.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size)!
    if self.view.frame.origin.y != 0{
        self.view.frame.origin.y += keyboardSize.height
    }

}
   override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}