MessageKit Not Showing Message Input Bar Swift 5
Asked Answered
H

2

5

So This is the controller hierarchy tabBarController -> some controller & chat channel controller.

and this chat channel controller is also a navigation controller. When I select row it pushes to chat controller which is of class MessageViewController.

I have 2 issues here one minor one major.

minor one is that the avatar.

    func avatarSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize {
        print("delegate method called")
        return .zero
}

I set it to zero in one of the delegates but it still shows.And also it never prints the statement.

major one is that the input bar is not showing at all. I already have InputBarAccessoryView in my pod file and

messageInputBar.delegate = self as InputBarAccessoryViewDelegate

extension ChatViewController: InputBarAccessoryViewDelegate {

    func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
        print("text")
    }

}

but nothing shows. I also checked the views and I couldn't find it chat view view hierachy

Homogeneous answered 20/9, 2019 at 20:45 Comment(0)
M
7

to resolve this I had to :

import InputBarAccessoryView

so you dont have to do:

messageInputBar.delegate = self as InputBarAccessoryViewDelegate

and in your viewDidLoad() set these delegates(colors optional)

        super.viewDidLoad()
         guard let userId = Auth.auth().currentUser?.uid else{return}
        Database.fetchUserWithUID(uid: userId) { (user) in
            self.currentLogginUser =  user
        }
        messageInputBar.delegate = self
        maintainPositionOnKeyboardFrameChanged = true
        messageInputBar.inputTextView.tintColor = .yellow
        messageInputBar.sendButton.setTitleColor(.purple, for: .normal)

        messagesCollectionView.messagesDataSource = self
        messagesCollectionView.messagesLayoutDelegate = self
        messagesCollectionView.messagesDisplayDelegate = self
        messagesCollectionView.messageCellDelegate = self

        messageInputBar.leftStackView.alignment = .center
        messageInputBar.setLeftStackViewWidthConstant(to: 50, animated: false)
    getMessages()
    }

then if using Realtime Database - Firebase


    func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
        guard let recipient = self.recipient else{return}
        guard let uid = Auth.auth().currentUser?.uid else{return}
        let ref = Database.database().reference().child("messages")
        let childRef = ref.childByAutoId()

        let values = ["sender": uid, "text": text, "recipient": recipient.uid, "time": Date().timeIntervalSince1970.description]
        childRef.updateChildValues(values) { (err, ref) in

            if let err = err{
                print(err)
                return
            }

            let userMessageRef = Database.database().reference().child("user-messages").child(uid).child(recipient.uid)

            //add chat id generated from AutoId
            guard let messageId = childRef.key else{return}
            userMessageRef.updateChildValues([messageId: 1])

            let recipientMessageRef = Database.database().reference().child("user-messages").child(recipient.uid).child(uid)
            recipientMessageRef.updateChildValues([messageId:1])
        }
        inputBar.inputTextView.text = ""

    }
}


Marchioness answered 23/9, 2019 at 19:30 Comment(0)
C
5

Solution for showing the input bar:

class ViewController: MessagesViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        self.becomeFirstResponder()
    }
}

Source: https://github.com/MessageKit/MessageKit/issues/501#issuecomment-361913168

Cowpox answered 20/10, 2019 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.