gpt4 book ai didi

swift - Input Accessory View Behave Unexpected with keyboard 隐藏和显示事件

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:58 26 4
gpt4 key购买 nike

我遇到了 InputAccessoryView 的奇怪行为,我正在使用 InputAccessoryView 的聊天屏幕上工作,我已经注册了 KeyboardWillShowKeyboardWillHide通知。当我的聊天屏幕出现时,它会自动调用 KeyboardWillShowMethod 一次,之后它会自动隐藏而不调用 KeyboardWillHide 通知。加载聊天后,当我单击文本框键入文本时,它会调用 KeyboardWillShow,这很好。但是当我尝试隐藏键盘时,它首先调用两个方法,它会调用 KeyboardWillHide,然后它会调用 KeyboardWillShow,这很奇怪。

这是我的聊天屏幕 Image隐藏键盘时。这是显示键盘的时间 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 插图,因为我没有得到 keybaord 的高度,这也是一个问题。与 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

})
}
}

关于swift - Input Accessory View Behave Unexpected with keyboard 隐藏和显示事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51355483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com