4

I have a question about the new NavigationStack in IOS 16. I have a problem setting navigationTitle in ContentView, I set navigationTitle but it is the same for all tabs. Can I set it somehow so that I can edit it for each different tab? Using tag ? thank you very much

struct ContentView: View {
@State var selection = 1
var body: some View {
    NavigationStack {
        TabView(selection: $selection) {
            LaunchView()
                .badge(2)
                .tabItem {
                    Label("Received", systemImage: "tray.and.arrow.down.fill")
                }
                .tag(1)
            DeView()
                .tabItem {
                    Label("Sent", systemImage: "tray.and.arrow.up.fill")
                }
                .tag(2)
            DeView()
                .tabItem {
                    Label("Sent", systemImage: "tray.and.arrow.up.fill")
                }
                .tag(3)
        }
        .navigationTitle("test")
    }
}

}

yaawl
  • 51
  • 1
  • 2

1 Answers1

2

You can create a function that returns the title for every selection:

func title() -> String {
    if selection == 1 {
        return title
    }else if selection == 2 {
        return some Title
    }else if selection == 3 {
        return some other Title
    }
} 

Or my personal best way: Enums!

Create an enum that holds the tabs, then create a title property for each tab:

struct ContentView: View {
    @State var selection = Tab.received
    var body: some View {
        NavigationStack {
            TabView(selection: $selection) {
                Text("hello")
                    .badge(2)
                    .tabItem {
                        Label("Received", systemImage: "tray.and.arrow.down.fill")
                    }
                    .tag(Tab.received)
                Text("hello3")
                    .tabItem {
                        Label("Sent", systemImage: "tray.and.arrow.up.fill")
                    }
                    .tag(Tab.sent)
                Text("hello5")
                    .tabItem {
                        Label("Sent", systemImage: "tray.and.arrow.up.fill")
                    }
                    .tag(Tab.sennt)
                    
            }.navigationTitle(selection.title)
        }
    }
}

enum Tab: Int {
    case received = 1
    case sent = 2
    case sennt = 3
    var title: String {
        switch self {
            case .received:
            return "Hello"
            case .sent:
            return "Hello World"
            case .sennt:
            return "Hello, World!"
        }
    }
}

Plus it’s easier to work with than Ints.

Edit: To hide the TabBar for DeviceView:

struct Test: View {
    @State var selection = 1
    @State var devicePresented = false
    var body: some View {
        NavigationStack {
            //Content
            .navigationDestination(isPresented: $devicePresented) {//present DeviceView when devicePresented is true
                DeviceView()
            }
        }
    }
}

struct SettingsView: View {
    @Binding var devicePresented: Bool
    var body: some View {
        List {
            Button(action: {
                devicePresented.toggle()
            }) {
                Text("Go to device")
            }
        }
    }
}
Timmy
  • 4,098
  • 2
  • 14
  • 34
  • Wow thank you very much ! Its work but I also have a toolbar on my first view and I would need to hide it for the other views. I'm solving all this because I can't hide the tab view in the navigation stack. Do you have any tips on how to make 3 views and the third view has settings where I need to click on the settings. – yaawl Sep 10 '22 at 07:38