ios - 当 AccessoryInputView 的键盘不存在时,键盘将显示调用的通知
问题描述
我有一个名为 chatBoxView 的输入附件视图,其中包含一个自定义的 growTextView 和几个用于发送消息的按钮。这个 inputAccessoryView 是在我的主视图控制器中设置的。
override var inputAccessoryView: UIView? {
get {
return chatBoxView
}
}
override var canBecomeFirstResponder: Bool {
return true
}
所以每次我去这个viewController,我都会在屏幕底部看到chatBoxView,这是我想要的。但是,键盘尚不可见(只有 chatBoxView),但为什么我的 KeyBoardWillShow 通知不断被调用?
此外,当键盘打开时,当我点击外部时消失,KeyboardWillDisappear 函数被调用,但 KeyBoardWillShow 函数被调用。
这是键盘通知的功能。
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
. 仅仅看键盘的高度是不够的。您需要查看键盘的位置——查看其整个帧信息。如果键盘的顶部不会高于屏幕底部,显然你不应该做任何事情。同样,如果键盘框架不会从原来的位置改变,你不应该做任何事情。
推荐阅读
- apache-camel - 如何在 JSLT 组件中使用函数选项?
- javascript - cowin api 问题。没有得到来自服务器的预期响应
- excel-formula - 从日期列表中获取即将到来的星期六
- html - 将css网格(显示:网格)的行对齐到顶部?
- python - 如何使用python将文本布局颜色更改为白色并将文本更改为黑色?
- wordpress - Woocommerce 在结帐按钮单击时显示模式
- javascript - 如何将选定的数据库列存储为 html 页面(nodejs - express)中的变量?
- mongodb - 使用另一个字段的值更新 MongoDB 数组字段值
- docker - 空手道测试 - 在 Docker 中运行时关键输入不起作用
- vuejs3 - Vue3 生命周期。当在另一个反应变量中使用时,反应变量不会更新