gpt4 book ai didi

swift - 导航栏隐藏在 JSQMessagesViewController 中

转载 作者:行者123 更新时间:2023-11-28 15:00:23 25 4
gpt4 key购买 nike

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

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




}

下面是对话 View Controller 之前的类中 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)

如果您没有将第一个 Controller 设置为导航 Controller 的 Root View Controller ,请确保事先这样做。

或者将 ConvoVC 设置为导航 Controller 的 Root View Controller 并呈现该导航 Controller

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

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

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

关于swift - 导航栏隐藏在 JSQMessagesViewController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48969508/

25 4 0