我在使用InputAccessoryView时遇到了奇怪的行为,我正在聊天屏幕上工作,我在其中使用了
InputAccessoryView
,在那里我注册了KeyboardWillShow
和KeyboardWillHide
通知。当我的聊天屏幕出现时,它会自动调用 KeyboardWillShowMethod
一次,之后它会自动隐藏,而不调用 KeyboardWillHide
通知。加载聊天后,当我单击文本框输入文本时,它会调用 KeyboardWillShow
,这很好。但是当我尝试隐藏键盘时,它首先调用两个方法,它会调用 KeyboardWillHide
,然后它会调用 KeyboardWillShow
,这很奇怪。
这是我的聊天屏幕图像当键盘隐藏时。 这是键盘显示的时候Image
我正在使用这个
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
})
}
}
在选择器中,我试图根据键盘的 Y 索引更改我的
CollectionView
插入,因为我没有得到键盘的高度,这也是一个问题。自 inputAccessoryView 高度起,kyeboard 的高度始终为 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
})
}
}
当键盘有输入附件视图时,会观察到两次
keyboardDidHideNotification
:
如果您的实现依赖于仅调用一次选择器,您可以执行以下解决方法之一:
选项A:检查键盘框架:
@objc
private func keyboardDidHide(_ notification: Notification) {
guard let keyboardRect = notification.keyboardRect, keyboardRect.origin.y == view.frame.maxY else {
return
}
// Do whatever you need...
}
extension Notification {
var keyboardRect: CGRect? {
guard let keyboardSize = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
return nil
}
return keyboardSize.cgRectValue
}
}
选项 B:使用 GDC 进行节流反应:
private var pendingKeyboardDidHideRequestWorkItem: DispatchWorkItem?
private func keyboardDidHide(_ notification: Notification) {
pendingKeyboardDidHideRequestWorkItem?.cancel()
let keyboardDidHideRequestWorkItem = DispatchWorkItem { [weak self] in
// Do whatever you need...
}
pendingKeyboardDidHideRequestWorkItem = keyboardDidHideRequestWorkItem
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500), execute: keyboardDidHideRequestWorkItem)
}
如果您有InputAccessoryView,请在键盘通知中检查键盘高度是否等于InputAccessoryView 高度。如果为 true,则更新隐藏键盘的视图,如果为 false,则在键盘可见时更新视图