swift 在iOS中关闭键盘

jyztefdp  于 2023-05-05  发布在  Swift
关注(0)|答案(5)|浏览(152)

我查看并尝试了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个文本字段,他们都设置为自我委托。按回车键时,它们的标记将递增,以移动到下一个文本字段。我不认为这会影响它。抱歉,新手!代码可以很好地运行这两个函数,但它们的键盘仍然存在。我只想解雇键盘时,触摸任何文本字段之外。

yizd12fk

yizd12fk1#

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

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

    }

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

而在视图的viewDidLoad中,任何视图控制器都有textField。

self.hideKeyboardWhenTappedAround()
h79rfbju

h79rfbju2#

Swift 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)
 }
}
xoefb8l8

xoefb8l83#

*以下是在键盘打开时关闭键盘并向上移动视图的解决方案:斯威夫特酒店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)
}
ulydmbyx

ulydmbyx4#

你有没有试着调试程序,看看代码是否在函数中停止(有断点)?通常这段代码应该可以工作...检查那些textFields是在super view还是在child view中,如果是,也许你应该调用self.childView.endEditing(true)
如果你真的使用多个textField,也许你应该尝试IQKeyboardManager库。我在我所有的项目中都使用它。你可以在这里找到它:https://github.com/hackiftekhar/IQKeyboardManager .使用简单,支持良好。只需通过可可pods安装它,将IQKeyboardManager.sharedManager().enable = true放入AppDelegate中,即可开始使用。:)

gwo2fgha

gwo2fgha5#

您确定正在调用touchesBegan吗?如果你确定,尝试将self.resignFirstResponder()添加到touchesBegan函数中。这告诉你的视图控制器它不再是第一个响应者,应该关闭键盘。
如果没有,您需要做的是创建一个UITapGestureRecognizer,将其添加到视图中,并将其连接到调用self.resignFirstResponder()的函数。

相关问题