目前,当键盘显示或隐藏时,我有一个使用键盘动画的视图。我添加了一个手势识别器,以便当用户点击键盘时它会消失。

我遇到的问题是,如果在出现键盘时用户轻按以放下键盘,键盘就会消失并且我的视野不会降低。我实际上已经注意到,无论出于何种原因,该视图都可以更高。

这是我的键盘侦听器方法:

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        if let tabBarController = tabBarController {
            responseNode.frame.origin.y -= keyboardSize.height-tabBarController.tabBar.frame.height
            tableNode.view.contentInset.bottom += keyboardSize.height-tabBarController.tabBar.frame.height
        }
    }
}
func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        if let tabBarController = tabBarController {
            responseNode.frame.origin.y += keyboardSize.height-tabBarController.tabBar.frame.height
            tableNode.view.contentInset.bottom -= keyboardSize.height-tabBarController.tabBar.frame.height
        }
    }
}

这是我隐藏键盘的方法:
extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)
    }

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

任何帮助将不胜感激!

最佳答案

尝试这个

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

func textFieldShouldReturn(textField: UITextField) -> Bool {

    text.resignFirstResponder()

    return true

}

08-27 19:07
查看更多