我有:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
以及:
func keyboardWillShow(notification: NSNotification) {
if (self.view.center.y == maxPointY){
self.view.frame.origin.y -= maxPointY/2
}
}
func keyboardWillHide(notification: NSNotification) {
self.view.frame.origin.y += maxPointY/2
}
问题是,当我单击一个应该弹出键盘的文本字段时,
keyboardWillHide
会被调用。我也有这个:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
usernameTextField.resignFirstResponder()
passwordTextField.resignFirstResponder()
textField.resignFirstResponder()
return true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
最佳答案
试试这个:斯威夫特3
它在我身边起作用。我想你没有经过辩论就通过了选择器
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
选择器
func keyboardWillShow(notification: NSNotification) {
}
func keyboardWillHide(notification: NSNotification) {
}
代表
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
关于ios - keyboardWillHide通知故障,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44838604/