2

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)
}
Hanzala Raza
  • 149
  • 1
  • 16

1 Answers1

1

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)
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • Thanks for the response , I am already pushing it , check the edit – Hanzala Raza Jul 24 '20 at 22:05
  • 1
    Is the navigation bar available before pushing? – Frankenstein Jul 25 '20 at 03:40
  • basically i have hidden the default navigation bar , wanted to show the navigation bar which is visible in second pic , its from message kit official demo app , I have copied the code from there but its not working that update title view function is for navigation bar which i have to put on – Hanzala Raza Jul 25 '20 at 09:39
  • 1
    I'm getting this same issue, except my navbar is showing but it's translucent (Meaning just the "back" button is showing in it, but my messages still go all the way to the top of the screen). Anyone know how to fix this? – Eric Feb 10 '22 at 19:09
  • 1
    @Eric i am facing the same issue. Did you find any solution for that? – Taimoor Arif Mar 05 '22 at 07:41
  • @TaimoorArif I don't remember exactly, but I think I never fully "solved" it so much as I had to settle for a workaround. I think it should be put as an *issue* in MessageKit. – Eric Mar 06 '22 at 23:11
  • @Eric I found the solution, its the issue with my **IQKeyboard**. I just write only two lines code in *App Delegate* and it worked fine at my side. – Taimoor Arif Mar 07 '22 at 10:11
  • @TaimoorArif Mind sharing the two lines of code you added? – Eric Mar 08 '22 at 02:37
  • 1
    @Eric you have to write this code in **didFinishLaunchingWithOptions** function of **AppDelegate** : `IQKeyboardManager.shared.enable = true IQKeyboardManager.shared.disabledDistanceHandlingClasses.append(YourChatVC.self) ` – Taimoor Arif Mar 08 '22 at 06:40