2017-09-28 91 views
0
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

func keyboardWillHide(notification: NSNotification) { 
    if let rect = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { 

    } 
} 

順便說一下,函數不會給出正確的RectValue。 這是怎麼回事?iOS11中的鍵盤問題

+0

什麼是錯的矩形你得到? – Vakas

+0

這不包含QuickTypeBar的高度。 – Wang90925

+0

哪種類型的酒吧?你在談論自動更正欄嗎? – Vakas

回答

0

試試這個:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

func keyboardWillHide(notification: NSNotification) { 
     if let rect = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { 

     } 
    } 
func keyboardWillShow(notification: NSNotification) { 
     if let rect = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 

     } 
    } 
+0

'UIKeyboardFrameEndUserInfoKey'應該在'keyboardWillHide'中,'UIKeyboardFrameBeginUserInfoKey'應該在'keyboardWillShow'中 – Vakas