yaawl
yaawl

Reputation: 61

SwiftUI navigationStack in tabView

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

}

Upvotes: 5

Views: 4511

Answers (1)

RelativeJoe
RelativeJoe

Reputation: 5084

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

Upvotes: 3

Related Questions