【问题标题】:MessageKit: the added input bar button only appears after I sent a messageMessageKit:添加的输入栏按钮仅在我发送消息后出现
【发布时间】:2021-01-25 12:08:46
【问题描述】:

我正在使用 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 {
                $0.spacing = .fixed(10)
                $0.image = UIImage(systemName: named)?.withRenderingMode(.alwaysTemplate)
                $0.setSize(CGSize(width: 25, height: 25), animated: false)
                $0.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)
    }
}


【问题讨论】:

    标签: ios swift messagekit


    【解决方案1】:

    由于以下代码而发生 -

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

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

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-27
      • 2022-11-22
      • 2021-12-09
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多