swiftuinavigationviewtabviewtabitemnavigationitem

I am not able to overwrite NavigationItemTitle into TabView in SwiftUI


I am a new in SwiftUI. I have a ContentView, in that I added NavigationView and from that NavigationView it redirect into another view which have a TabView.

//ContentView.swift

struct ContentView: View {

  @State private var isValidLogin : Bool = false    

  var body: some View {
    NavigationView{
         VStack
         {
           NavigationLink(destination: HomePage(), isActive:$isValidLogin) {
                Text("LOGIN")
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .onTapGesture {
                         self.isValidLogin = true
                    }
          }
        }.padding()
      }
   }
} 

Here Code for HomePage.swift

import SwiftUI

struct HomePage: View {

private enum Tab: Hashable {
    case home
    case map
}
@State private var selectedTabbar : Tab = .home

var body: some View {
    TabView(selection: $selectedTabbar)
    {
        HomeView()
            .tag(0)
            .tabItem{
                Text("Home")
                Image(systemName: "house.fill")
            }
        
        MapView()
            .tag(1)
            .tabItem {
                Text("Map")
                Image(systemName: "map")
            }
    }.navigationBarTitle("Settings")
 }
}

struct HomePage_Previews: PreviewProvider {
    static var previews: some View {
        HomePage()
    }
}

Here Code for MapView.swift is here

import SwiftUI

struct MapView: View {

var body: some View {
    Text("You are in Map Page")
        .navigationBarTitle(Text("Map"), displayMode: .inline)
 }
} 
struct MapView_Previews: PreviewProvider {
    static var previews: some View {
        MapView()
    }
}

My Error is : When I am setting global title to TabView property .navigationBarTitle("Settings") is always reflecting. But I want like that if I am setting the NavigationTitle into child view then it will display in that child view. I need like that when we tap on to Map Item that time NavigationTitle should Map. I am not able to find error why it is not working when we apply navigation title into child.

Any help should be Appreciate. Thanks in advance.


Solution

  • I found the solution.

    Here I changed my HomePage:

    struct HomePage: View {
        @State private var selectedTabbar = 0
        
        var body: some View {
            TabView(selection: $selectedTabbar)
            {
                HomeView()
                    .tag(0)
                    .tabItem{
                        Text("Home")
                        Image(systemName: "house.fill")
                    }
                MapView()
                    .tag(1)
                    .tabItem {
                        Text("Map")
                        Image(systemName: "map")
                    }  Image(systemName: "gear")
                    }
            }.navigationBarTitle(Text(navigationBarTitle))
            .navigationBarBackButtonHidden(true)
            .accentColor(.red)
        }
    }
    

    And I added one extension for HomePage. It is setting the child view's navigation title:

    private extension HomePage {
        var navigationBarTitle: String {
            selectedTabbar ==  0 ? "Home" : "Map"
        }
    }
    

    Using this extension we can set the dynamically navigation title into TabView.