2016-07-05 14 views
1

當鍵盤顯示或隱藏時,我有一個使用鍵盤進行動畫處理的視圖。我添加了一個手勢識別器,以便當用戶點擊鍵盤時消失。KeyboardWillHide在KeyboardWillShow仍在動畫時調用

我遇到的問題是,如果用戶在鍵盤出現時放開鍵盤以降低鍵盤,鍵盤消失並且我的視圖不會降低。我實際上已經注意到,無論出於何種原因,視圖都會更高。

這裏是我的鍵盤監聽器方法:

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

任何幫助將不勝感激!

+0

如果可能的話,請嘗試使用自動佈局,我想改變框架可能是在這種情況下不可預知的。 – juanjo

回答

2

試試這個

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

func textFieldShouldReturn(textField: UITextField) -> Bool { 

    text.resignFirstResponder() 

    return true 

} 
+0

這會隱藏鍵盤 –

相關問題