在KeyboardWillShow仍然是动画时调用KeyboardWillHide

时间:2016-07-05 23:37:23

标签: ios swift keyboard

目前,当键盘显示或隐藏时,我可以使用键盘进行动画制作。我添加了一个手势识别器,以便当用户轻敲键盘时它会消失。

我遇到的问题是,如果用户在键盘出现时轻敲以降低键盘,键盘会消失,我的视图也不会降低。我实际上已经注意到,无论出于何种原因,视图都会变得更高。

以下是我的键盘监听器方法:

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

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

试试这个

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

func textFieldShouldReturn(textField: UITextField) -> Bool {

    text.resignFirstResponder()

    return true

}