0

My code is currently seen below. I'm expecting the first tab to have the navigation title "Friends" and the second tab to have the navigation title "Filtered View". However, they both show the navigation title of "Friends" (https://i.stack.imgur.com/WZQ5S.png). I've also tried various alternatives and gotten things like two navigation titles being shown on the same screen (https://i.stack.imgur.com/zAnEs.png). How do I get the first tab to have the title of "Friends" and the second tab to have the title of "Filtered View"? Thank you very much!

struct ContentView: View {
    @Binding var friends: [Friend]
    
    var body: some View {
        TabView {
            DatabaseView(friends: $friends) {
                Task {
                    do {
                        try await FriendStore.save(friends: friends)
                    } catch {
                        print("Try again later.")
                    }
                }
            }
                .tabItem {
                    Label("Everyone", systemImage: "person.3")
                }
            
            FilteredView()
                .tabItem {
                    Label("Filtered", systemImage: "checkmark.circle")
                }
        }
    }
}
struct DatabaseView: View { 
    @Binding var friends: [Friend]
    var body: some View {
        List {
            ForEach($friends) { $friend in
                NavigationLink(destination: DetailView(friend: $friend)) {
                    CardView(friend: friend)
                }
            }
        .navigationTitle("Friends")
        .sheet(isPresented: $isPresentingNewDatabaseView) {
                NavigationView {
                    DetailEditView(data: $newFriendData)
                        .toolbar {
        ...
}
struct FilteredView: View {
    var body: some View {
        NavigationView {
            Text("Hello, World!")
        }
        .navigationTitle("Filtered View")
    }
}
Joshua C
  • 21
  • 3
  • 1
    Create a `NavigationView` for each `tabItem` – lorem ipsum Sep 03 '22 at 18:21
  • I tried that but I don't think it works. I also tried the approaches here (https://stackoverflow.com/questions/67237313/navigationview-inside-a-tabview-swift-ui) and here (https://stackoverflow.com/questions/65996670/navigationview-inside-tabview-how-to-get-to-top-level-tabview-when-using-the-ta) but they don't work – Joshua C Sep 04 '22 at 05:10
  • It does, Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. Make sure the `NavigationView`s are for each `tabItem` and make sure you don't have one above the `TabView` – lorem ipsum Sep 04 '22 at 11:24
  • Oh I understand. I had a `NavigationView` above the `TabView` which was causing the problem. I also used https://stackoverflow.com/questions/67237313/navigationview-inside-a-tabview-swift-ui. Thank you very much for your help! – Joshua C Sep 05 '22 at 06:36

0 Answers0