Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessageKit Not Showing Message Input Bar Swift 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

like image 581
Dayson D Avatar asked Mar 03 '23 09:03

Dayson D


2 Answers

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 = ""

    }
}


like image 172
Di_Nerd Avatar answered Mar 20 '23 02:03

Di_Nerd


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

like image 31
Savchenko Dmitry Avatar answered Mar 20 '23 00:03

Savchenko Dmitry