iosswiftmessagekit

messageInputBar not dismissing when I dismiss my MessagesViewController (MessageKit, begging for help!)


I am using MessageKit. I've created a MessagesViewController. I add messageInputBar as a subview from viewDidLoad along with a navigational bar that includes a back button. Whenever I am in this view controller and I tap on the messageInputBar's text field and then tap the back button, the messageInputBar stays on the screen when the app goes back to the previous UIViewController. If I don't tap on the messageInputBar when i first enter the MessagesViewController and press the back button, the messageInputBar properly is dismissed. Below is my code

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpNavBar()

        navigationItem.largeTitleDisplayMode = .never
        maintainPositionOnKeyboardFrameChanged = true
        scrollsToLastItemOnKeyboardBeginsEditing = true

        messageInputBar.inputTextView.tintColor = .systemBlue
        messageInputBar.sendButton.setTitleColor(.systemTeal, for: .normal)

        messageInputBar.delegate = self
        messagesCollectionView.messagesDataSource = self
        messagesCollectionView.messagesLayoutDelegate = self
        messagesCollectionView.messagesDisplayDelegate = self

        loadChat()

        self.view.addSubview(messageInputBar)

        messageInputBar.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            messageInputBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
            messageInputBar.widthAnchor.constraint(equalToConstant: self.view.bounds.width)
        ])

        NSLayoutConstraint.activate([
            messagesCollectionView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100)
        ])

    }

    func setUpNavBar() {
        let navBar = UINavigationBar()
        self.view.addSubview(navBar)
        navBar.items?.append(UINavigationItem(title: (selectedUser?.userFirstName)!))
        let backButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(backButtonTapped))
        navBar.topItem?.leftBarButtonItem = backButton

        navBar.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            navBar.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
            navBar.heightAnchor.constraint(equalToConstant: 44),
            navBar.widthAnchor.constraint(equalToConstant: self.view.bounds.width)
        ])
    }

    @IBAction func backButtonTapped(_ sender: Any) {
        let transition = CATransition()
        self.view.window!.layer.add(transition.segueLeftToRight(), forKey: kCATransition)

        self.dismiss(animated: false)
    }

Solution

  • I installed MessageKit using Cocoapods later to find out they dropped support for Cocoapods. So I completely migrated my entire project over to Swift Package Manager to get the latest MessageKit which includes the setup for the inputbar in their code. No idea why they would release a version that didn't have this initially? Anyways, solved my problem!