导航栏隐藏在 JSQMessagesViewController 中

Navigation Bar is Hidden in JSQMessagesViewController

我正在努力将聊天功能整合到我的应用程序中,使用 JSQMessagesViewController 一切正常,除了我无法在视图控制器中显示导航栏。我已经尝试了几乎所有看起来像的东西,但它似乎被视图或其他东西隐藏了,尽管我不确定这一点。这是我的对话视图控制器代码:

import UIKit
import JSQMessagesViewController
import Firebase
import AVKit

class ConvoVC: JSQMessagesViewController {

var chatRoomId = Variables.chatRoomID
var outgoingBubbleImageView: JSQMessagesBubbleImage!
var incomingBubbleImageView: JSQMessagesBubbleImage!
var messages = [JSQMessage]()

override func viewDidLoad() {
    super.viewDidLoad()

    senderId = uid!
    senderDisplayName = uid!

    let factory = JSQMessagesBubbleImageFactory()

    incomingBubbleImageView = factory?.incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleLightGray())
    outgoingBubbleImageView = factory?.outgoingMessagesBubbleImage(with: UIColor.jsq_messageBubbleBlue())

    collectionView.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
    collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero

    let query =  ref.child("ChatRooms").child(chatRoomId!).child("Messages").queryLimited(toLast: 10)

    query.observe(.childAdded, with: { (snapshot) in
        if snapshot.exists(){
        if  let data        = snapshot.value as? [String: String],
            let id          = data["sender_id"],
            let name        = data["name"],
            let text        = data["text"],
            !text.isEmpty
        {
            if let message = JSQMessage(senderId: id, displayName: name, text: text)
            {
                self.messages.append(message)

                self.finishReceivingMessage()
            }
        }
    }
    })

    //let navigationBar = UINavigationBar(frame: CGRect(0, 0, self.view.frame.size.width, 64)) // Offset by 20 pixels vertically to take the status bar into account

    //navigationBar.backgroundColor = UIColor.white
    //navigationBar.delegate = self as! UINavigationBarDelegate

    // Create a navigation item with a title
    //let navigationItem = UINavigationItem()
    //navigationItem.title = contacts[i].firstName

    // Create right button for navigation item
    setupBackButton()

    // Make the navigation bar a subview of the current view controller
    //self.view.addSubview(navigationBar)

}


override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    self.navigationController?.isNavigationBarHidden = false
}
func setupBackButton() {
    let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(backButtonTapped))
    navigationItem.leftBarButtonItem = backButton
}
@objc func backButtonTapped() {
    dismiss(animated: true, completion: nil)
}



override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
    let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell

    let message = messages[indexPath.item]

    if message.senderId == senderId {
        cell.textView!.textColor = UIColor.white
    } else {
        cell.textView!.textColor = UIColor.black
    }

    return cell
}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
    return messages[indexPath.item]
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return messages.count

}



override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) {
    let messageRef = ref.child("ChatRooms").child(chatRoomId!).child("Messages").childByAutoId()


    let message = ["sender_id": senderId, "name": senderDisplayName, "text": text]

    messageRef.setValue(message)

    finishSendingMessage()
    JSQSystemSoundPlayer.jsq_playMessageSentSound()
    self.finishSendingMessage()

}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {
    let message = messages[indexPath.item]
    if message.senderId == senderId {
        return outgoingBubbleImageView
    }else {
        return incomingBubbleImageView
    }
}




}

这是对话视图控制器之前 class 中 didSelectRow 方法的代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let chatFunctions = ChatFunctions()

    ref.child("users").child(uid!).observeSingleEvent(of: .value) { (snapshot) in
        let userOne = User(snapshot: snapshot)
        self.chatRoomIdentity = chatFunctions.startChat(user1: userOne , user2: self.matches[indexPath.row])
        self.senderDisName = self.matches[indexPath.row].firstName
        Variables.chatRoomID = self.chatRoomIdentity

        self.present(ConvoVC(), animated: true, completion: nil)

    }




}

提前致谢!

您在这一行以模态方式呈现它,这将全屏呈现它(因此没有导航栏):

self.present(ConvoVC(), animated: true, completion: nil)

将其推送到导航堆栈,而不是像这样:

self.navigationController?.pushViewController(ConvoVC(), animated: true)

如果您没有将第一个控制器设置为导航控制器的根视图控制器,请确保事先这样做。

或者将 ConvoVC 设置为导航控制器的根视图控制器并显示该导航控制器

self.present(navigationController, animated: true, completion: nil).

第一个解决方案会让您弹出 ConvoVC 并通过导航栏返回到之前的视图控制器,而后者不会(您必须关闭它)。

我强烈建议阅读 iOS 导航和转场,因为它是任何 iOS 应用程序的核心方面。