BiggRojo
BiggRojo

Reputation: 31

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)
    }

Upvotes: 0

Views: 282

Answers (2)

BiggRojo
BiggRojo

Reputation: 31

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!

Upvotes: 1

tanmoy
tanmoy

Reputation: 1438

As you have created MessagesViewController, you don't need to explicitly add messageInputBar to the bottom of the view.

Let's look at the source of MessageKit

private func setupInputBar(for kind: MessageInputBarKind) {
    inputContainerView.subviews.forEach { $0.removeFromSuperview() }

    func pinViewToInputContainer(_ view: UIView) {
      view.translatesAutoresizingMaskIntoConstraints = false
      inputContainerView.addSubviews(view)

      NSLayoutConstraint.activate([
        view.topAnchor.constraint(equalTo: inputContainerView.topAnchor),
        view.bottomAnchor.constraint(equalTo: inputContainerView.bottomAnchor),
        view.leadingAnchor.constraint(equalTo: inputContainerView.leadingAnchor),
        view.trailingAnchor.constraint(equalTo: inputContainerView.trailingAnchor),
      ])
    }

    switch kind {
    case .messageInputBar:
      pinViewToInputContainer(messageInputBar)
    case .custom(let view):
      pinViewToInputContainer(view)
    }
  }

The following code section should be removed from your source as the messageInputBar has already been set up in the library.

 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)
        ])

Now, your scenario

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.

Whenever, there is an object that you interact with (eg. messageInputBar), and it is not deallocated (stays in view) after you dismissed the view controller, there is a memory leak.

If you repeatedly enter and dismiss the view controller, you will observe a rise in the memory usage of the app. So, finding out which object is creating this retain cycle, should solve this issue.

Upvotes: 1

Related Questions