[英]Keyboard will show notification called when keyboard is not present for AccessoryInputView
我有一個名為 chatBoxView 的輸入附件視圖,其中包含一個自定義的 growTextView 和幾個用於發送消息的按鈕。 這個 inputAccessoryView 設置在我的主視圖 controller 中。
override var inputAccessoryView: UIView? {
get {
return chatBoxView
}
}
override var canBecomeFirstResponder: Bool {
return true
}
所以每次我 go 到這個 viewController 時,我都會在屏幕底部看到 chatBoxView,這是想要的。 但是,鍵盤尚不可見(只有 chatBoxView),但為什么我的 KeyBoardWillShow 通知不斷被調用?
此外,當鍵盤打開時,當我點擊外部時消失,KeyboardWillDisappear function 被調用,但 KeyBoardWillShow function 被調用。
這是鍵盤通知的 function。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(notification: Notification) {
if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
print("Notification: Keyboard will show")
DispatchQueue.main.async {
self.tableView.setBottomInset(to: keyboardHeight - self.chatBoxView.frame.height)
self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .bottom, animated: true)
}
}
}
@objc func keyboardWillHide(notification: Notification) {
print("Notification: Keyboard will hide")
tableView.setBottomInset(to: 0.0)
}
所以基本上,當鍵盤沒有顯示並且只有附件視圖時,我得到了日志“通知:鍵盤將顯示”。 有任何想法嗎?
問題是您丟棄了UIResponder.keyboardFrameEndUserInfoKey
中的重要信息。 僅僅看鍵盤的高度是不夠的。 您需要查看鍵盤的位置——查看其整個幀信息。 如果鍵盤的頂部不會高於屏幕底部,顯然你不應該做任何事情。 同樣,如果鍵盤框架不會從原來的位置改變,你不應該做任何事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.