swift - 输入附件视图行为异常与键盘隐藏和显示事件
问题描述
我在使用 InputAccessoryView 时遇到了一个奇怪的行为,我正在使用InputAccessoryView
我注册过的聊天屏幕KeyboardWillShow
和KeyboardWillHide
通知。当我的聊天屏幕出现时,它会自动调用KeyboardWillShowMethod
一次,之后它会自动隐藏而不调用KeyboardWillHide
通知。加载聊天后,当我单击文本框键入它调用的文本时,KeyboardWillShow
这很好。但是当我尝试隐藏 keybaord 时,它首先调用两个方法KeyboardWillHide
,然后它会调用KeyboardWillShow
,这很奇怪。
我正在以编程方式使用此InputAccessoryView
代码inputAccessoryView
这就是我注册键盘通知的方式。
func handleKeyBoard(){
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
var contentInset = self.collectionView?.contentInset
contentInset?.bottom = keyboardSize.maxY
self.collectionView?.contentInset = contentInset!
self.collectionView?.scrollIndicatorInsets = contentInset!
// collectionViewBottomAnchor?.constant = keyboardSize.height + 50
// print ("input height \(inputAccessoryView?.frame.maxY) ")
// print("keyboard height \(keyboardSize.height)")
// print("keyboard Y \(keyboardSize.maxY)")
// print("keyboard Y \(keyboardSize.minY)")
//print("keyboard Y \(inputAccessoryView.framemaxY)")
if self.messages.count > 0{
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed:Bool) in
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView?.scrollToItem(at: indexPath, at: .bottom, animated: true)
})
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
print("keyboard hide")
self.collectionView?.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 52, right: 0)
self.collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 8, left: 0, bottom: 52, right: 0)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed:Bool) in
})
}
}
在选择器中,我试图CollectionView
根据键盘的 Y 索引更改我的插入,因为我没有得到键盘的高度,这也是一个问题。键盘的高度始终为 inputAccessoryView 高度的 50。
解决方案
这是我找到的解决方案,感谢@Amit。UIKeyboardFrameBeginUserInfoKey
在这样做之后,我没有使用我使用过的方法,而是UIKeyboardFrameEndUserInfoKey
能够在方法中获得准确的键盘高度KeyboardWillAppear
。现在剩下的问题是该KeyboardWillShow
方法被调用,KeyboardWillHide
但当时KeyboardWillShow
键盘的高度为 50。这意味着当我尝试隐藏键盘时,它会调用 KeyboardWillHide,这很好,之后它会自动调用KeyboardWillShow
但键盘的高度仍然是 50,所以我把条件放在那里。
现在下面的方法只有在高度大于 50 时才会生效。
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if keyboardSize.height > 50{
var contentInset = self.collectionView?.contentInset
contentInset?.bottom = keyboardSize.height + 50
self.collectionView?.contentInset = contentInset!
self.collectionView?.scrollIndicatorInsets = contentInset!
if self.messages.count > 0{
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed:Bool) in
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView?.scrollToItem(at: indexPath, at: .bottom, animated: true)
})
}
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
self.collectionView?.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 52, right: 0)
self.collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 52, right: 0)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed:Bool) in
})
}
}
推荐阅读
- java - JFugue 笔记更改无法正常工作
- visual-c++ - 由于 exe 中缺少依赖项但存在于 dll 上,因此无法将 dll 从导入到我的 exe 进程
- python - 如何根据数组从(地理)熊猫数据框中选择多行或传播聚类算法结果的元数据?
- html - 模态不会从 href 链接打开
- eloquent - 如何在laravel中检索一对多关系表的最后一条记录
- android - 错误:客户端必须在 Pre-Marshmallow 上有 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION
- arrays - 在while循环中将数据添加到多维数组
- php - Wordpress 将正斜杠转换为反斜杠
- r - 我怎样才能得到所有分区的列表与K维度R
- sybase - 如何在 Sybase 中启用行内 (LOB) 存储以及何时考虑启用它?