首页 > 解决方案 > 输入附件视图行为异常与键盘隐藏和显示事件

问题描述

我在使用 InputAccessoryView 时遇到了一个奇怪的行为,我正在使用InputAccessoryView我注册过的聊天屏幕KeyboardWillShowKeyboardWillHide通知。当我的聊天屏幕出现时,它会自动调用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。

标签: swiftkeyboard-eventsinputaccessoryview

解决方案


这是我找到的解决方案,感谢@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

            })
        }
    }

推荐阅读