MessageKit:添加的输入栏按钮仅在我发送消息后出现

MessageKit: the added input bar button only appears after I sent a message

我正在使用 MessageKit 构建应用程序。

我在输入栏中添加了一个 InputBarItem,但是当视图第一次出现时它没有出现。只有在我按下“发送”后,该项目才会出现。

下面是 ChatViewController 的一部分。知道为什么会这样吗?

谢谢!

class ChatViewController: MessagesViewController {
    
    ....

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
       ...
        
        messageInputBar.delegate = self
    
        setupConstraints()
    }

}

extension ChatViewController: MessagesDataSource {
    ...
}


// MARK: - MessageInputBarDelegate

extension ChatViewController: InputBarAccessoryViewDelegate {

    @objc
    func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
        processInputBar(messageInputBar)
    }

    private func makeButton(named: String) -> InputBarButtonItem {
        return InputBarButtonItem()
            .configure {
                [=10=].spacing = .fixed(10)
                [=10=].image = UIImage(systemName: named)?.withRenderingMode(.alwaysTemplate)
                [=10=].setSize(CGSize(width: 25, height: 25), animated: false)
                [=10=].tintColor = .blue
            }
    }
    
    func processInputBar(_ inputBar: InputBarAccessoryView) {
        let components = inputBar.inputTextView.components
        
        inputBar.inputTextView.text = String()
        let items = [
            makeButton(named: "plus")
        ]
        inputBar.setLeftStackViewWidthConstant(to: 36, animated: false)
        inputBar.setStackViewItems(items, forStack: .left, animated: false)
    }
}


这是因为以下代码而发生的 -

@objc
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
    processInputBar(messageInputBar)
}

如果您在此处看到委托函数显示 didPressSendButtonWith 并且您正在调用您的函数以在此处添加项目。因此,在按下发送后,您的函数将被调用,将项目添加到输入栏。

谢谢。