2

Due to application specific reasons I have to nest a TabView in a NavigationView. But then the navigation bar title of the tab items doesn't get displayed, just an empty navigation bar.

Any solutions to this?

struct ContentView: View {
    var body: some View {
        NavigationView {
            TabView {
                Text("Tab 1")
                .navigationBarTitle("Tab 1") // is ignored, only an empty string is displayed
                .tabItem {
                    Text("Tab 1")
                }
                
                Text("Tab 2")
                .navigationBarTitle("Tab 2") // is ignored, only an empty string is displayed
                .tabItem {
                    Text("Tab 2")
                }
            }
            // this would display a navigation bar title, but then the title is the same for all tab items
            //.navigationBarTitle("TabView title")
        }
    }
}
dankito
  • 958
  • 7
  • 16

2 Answers2

2

Here is possible solution. Tested with Xcode 11.4 / iOS 13.4

struct ContentView: View {
    @State private var title = ""
    var body: some View {
        NavigationView {
            TabView {
                Text("Tab 1")
                .onAppear { self.title = "Tab 1" }
                .tabItem {
                    Text("Tab 1")
                }

                Text("Tab 2")
                .onAppear { self.title = "Tab 2" }
                .tabItem {
                    Text("Tab 2")
                }
            }
            .navigationBarTitle(title)
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • https://stackoverflow.com/questions/71524270/swiftui-navigationview-nests-tabview-auto-pop This kind of nesting will cause the page to automatically exit to the heel. Is there any way to solve it? – gaohomway Mar 18 '22 at 08:36
1

if let NavigationView outside TabView ,when you push a new View and change current App,when you return you app ,if will always pop to TabView

bugmaker
  • 11
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 09 '22 at 06:44
  • https://stackoverflow.com/questions/71524270/swiftui-navigationview-nests-tabview-auto-pop I asked, is there any way to solve it? – gaohomway Mar 18 '22 at 08:35