Search code examples
swiftswiftuiviewcontroller

swiftUI navigation bar title setting when using with viewcontroller


cells page

ChatView

here is my cells page code

    var body : some View{
        NavigationView {
            List(getPairsUser()){i in

                NavigationLink(destination: ChatView(friend:i)){
                    cellView(user : i)
                }
                .navigationBarTitle(Text("Friends List"),displayMode: .inline)
            }
                .navigationBarHidden(true)


        }.frame(minWidth: UIScreen.main.bounds.width, idealWidth: UIScreen.main.bounds.width, maxWidth: UIScreen.main.bounds.width,  maxHeight: .infinity)
            .clipShape(Rounded())

    }

in the navigationLink destination is ChatView()

and ChatView's code is here

struct ChatView: UIViewControllerRepresentable {

    @EnvironmentObject var obser:Observer


    var db = Firestore.firestore()
    var friend:User!
    var messageRef:CollectionReference!
    var pairRef:CollectionReference!

    init(friend:User){
        self.friend = friend

    }

    func updateUIViewController(_ uiViewController: ChatViewController, context: Context) {
        if(uiViewController.left){
            if(!self.obser.messageListenerFlag[friend.pairUid]!){
                self.obser.messageListenerFlag[friend.pairUid] = true
                if( uiViewController.lastMessageDate != nil){
                    self.obser.pairLastMessages[friend.pairUid] = uiViewController.lastMessage
                    self.obser.pairLastMessagesDate[friend.pairUid] = uiViewController.lastMessageDate
                }
            }

        }

    }
    func makeUIViewController(context: Context) -> ChatViewController {
        let chatViewController = ChatViewController()
        chatViewController.navigationItem.title = self.friend.name

        self.navigationBarTitle(Text(self.friend.name))


        chatViewController.title = self.friend.name
        chatViewController.senderId = self.obser.__THIS__.Uid
        chatViewController.senderDisplayName = self.obser.__THIS__.Name
        chatViewController.friend = self.friend
        chatViewController.messageRef = self.db.collection("pairs").document(self.friend.pairUid).collection("messages")
        chatViewController.pairRef = self.db.collection("pairs").document(self.friend.pairUid).collection("typingIndicator")
        chatViewController.__THIS__ = self.obser.__THIS__

        let _url = URL(string: self.friend.image)!
        if let data = try? Data(contentsOf: _url) {
            chatViewController.friendImage = UIImage(data: data)
        }

        self.obser.messageListenerFlag[friend.pairUid] = false




        self.obser.unreadMessageCount[friend.pairUid]? = 0


        return chatViewController
    }

    typealias UIViewControllerType = ChatViewController

}

and what I want to do is when getting into ChatView

I want add some text (friend's name) in the orange block (in the picture)

I already try to add .navigationTitle on cells page, but it doesn't work

also I try to modify viewcontroller's title, but it doesn't work too

how could I do?


Solution

  • Before I answer to your question, sorry for doing without any test. It's just based on my assumption.

    Set chatViewController.navigationItem.title = self.friend.name instead of call navigationBarTitle of the SwiftUI view.

    I think the pushed view is not normal SwiftUI but UIKit view view so you should handle it in UIKit manner.