Messagekit Navigation bar is not showing up

993 Views Asked by At

My Screen looking like this

How can i show the default or custom navigation bar ? tried everything but nothing works , It seems like nothing is on the top , its a messageViewcontroller of messagekit and couldn't find any delegate method for navigation bar , it would be nice if someone educate me about this ..

My Code

    override func viewDidLoad() {
    messagesCollectionView = MessagesCollectionView(frame: .zero, collectionViewLayout: CustomMessagesFlowLayout())
    messagesCollectionView.register(CustomCell.self)
    super.viewDidLoad()
    messagesCollectionView.messagesDataSource = self
    messagesCollectionView.messagesLayoutDelegate = self
    messagesCollectionView.messagesDisplayDelegate = self
    messagesCollectionView.messageCellDelegate = self
    messageInputBar.delegate = self
    configureMessageInputBar()
    configureInputBarItems()
    updateTitleView(title: "Hanzala", subtitle: "Online")
}


import UIKit

extension UIViewController {

func updateTitleView(title: String, subtitle: String?, baseColor: UIColor = .white) {
    
    let titleLabel = UILabel(frame: CGRect(x: 0, y: -2, width: 0, height: 0))
    titleLabel.backgroundColor = UIColor.clear
    titleLabel.textColor = baseColor
    titleLabel.font = UIFont.systemFont(ofSize: 15)
    titleLabel.text = title
    titleLabel.textAlignment = .center
    titleLabel.adjustsFontSizeToFitWidth = true
    titleLabel.sizeToFit()
    
    let subtitleLabel = UILabel(frame: CGRect(x: 0, y: 18, width: 0, height: 0))
    subtitleLabel.textColor = baseColor.withAlphaComponent(0.95)
    subtitleLabel.font = UIFont.systemFont(ofSize: 12)
    subtitleLabel.text = subtitle
    subtitleLabel.textAlignment = .center
    subtitleLabel.adjustsFontSizeToFitWidth = true
    subtitleLabel.sizeToFit()
    
    let titleView = UIView(frame: CGRect(x: 0, y: 0, width: max(titleLabel.frame.size.width, subtitleLabel.frame.size.width), height: 30))
    titleView.addSubview(titleLabel)
    if subtitle != nil {
        titleView.addSubview(subtitleLabel)
    } else {
        titleLabel.frame = titleView.frame
    }
    let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
    if widthDiff < 0 {
        let newX = widthDiff / 2
        subtitleLabel.frame.origin.x = abs(newX)
    } else {
        let newX = widthDiff / 2
        titleLabel.frame.origin.x = newX
    }
    
    navigationItem.titleView = titleView
}

}

I want the navigation bar like this

 Messagekit example navigation bar

Pushing ChatViewController in NavigationController

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let model = Api.Params.chatUser[indexPath.row]
    openConversation(model)
}

func openConversation(_ model: ChatUser) {
    Api.Params.inputRecieverId = model.userId
    let id = String(requestManager.instance.userID)
    let vc = ChatViewController(recieverId: model.userId, senderId: id, conversationId: "Eman-Conversation-\(id)-\(model.userId)")
    vc.title = model.username
    vc.navigationItem.largeTitleDisplayMode = .never
    navigationController?.pushViewController(vc, animated: true)
}
1

There are 1 best solutions below

9
On

You've to embed the MessageViewcontroller inside a UINavigationController and use it.

let navigationController = UINavigationController(rootViewController: MessageViewcontroller())

If the controller that you're pushing MessageViewcontroller onto already has a navigationController then push the MessageViewcontroller into the navigational stack instead of presenting.

let messageViewcontroller = MessageViewcontroller()
navigationController?.pushViewController(messageViewcontroller, animated: true)