Messagekit: How to hide keyboard when tap on messagesCollectionView on 3.4.2

Created on 29 Oct 2020  路  7Comments  路  Source: MessageKit/MessageKit

I add a UITapGestureRecognizer into messagesCollectionView in my custom viewcontroller which extends MessagesViewController.
I also return true in delegate method of UIGestureRecognizerDelegate.

 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {`
    return true
 } 

The above method is called but my selector method of above tap gesture is not called.
Here is my code:
Screen Shot 2020-10-29 at 23 15 02

  • Version of MessageKit: 3.4.2
question

All 7 comments

The same question, solutions, that you gave earlier, don't work in 3.4.2

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

i use 3.1.0 also cannot hide keyboard when tap on

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

You can leverage the existing gesture recognizer method in MessagesCollectionView by creating your own custom class that inherits MessagesCollectionView. Add a simple delegate and you can detect any taps in messagesCollectionView without interrupting the MessageCellDelegate methods.

protocol MessagesCollectionViewDelegate: AnyObject {
    func didTap()
}

class ChatMessagesCollectionView: MessagesCollectionView {
    weak var messagesCollectionViewDelegate: MessagesCollectionViewDelegate?

    override func handleTapGesture(_ gesture: UIGestureRecognizer) {
        super.handleTapGesture(gesture) // Required for MessageCellDelegate methods to work
        messagesCollectionViewDelegate?.didTap()
    }
}

Now just implement the delegate method in your MessagesViewController and hide the keyboard when the delegate is triggered.

class ChatMessagesViewController: MessagesViewController {
    override func viewDidLoad() {
        self.messagesCollectionView = ChatMessagesCollectionView()
        (self.messagesCollectionView as? ChatMessagesCollectionView)?.messagesCollectionViewDelegate = self
        super.viewDidLoad()
    }
}

extension ChatMessagesViewController: MessagesCollectionViewDelegate {
    func didTap() {
        self.messageInputBar.inputTextView.resignFirstResponder()
    }
}

extension YourMessageVC: MessageCellDelegate {
func didTapBackgroud(in cell: MessageCollectionViewCell) {
self.messageInputBar.inputTextView.resignFirstResponder()
}
}

Examples above should work 馃憤

Was this page helpful?
0 / 5 - 0 ratings