Home > Mobile >  Navigation Title not showing if View inside TabView
Navigation Title not showing if View inside TabView

Time:12-09

I have a content view like below. I add a FirstView that has tab view inside of it pointing to 2 separate view. however my problem is the NavigationTitle just does not show up no matter what I do, is it normal or an error on my part

struct ContentView: View {
    @ObservedObject var data: DataController
    var body: some View {
             FirstView(dataController: data)
  }
}

Then the First View has 2 tab items inside TabView

import SwiftUI

struct FirstView: View {
    @ObservedObject var dataController: DataController
    var body: some View {
        NavigationStack {
            TabView {
               
                MyAdsPageView(dataAd: dataController)
                        .tabItem {
                            Label("Home", systemImage: "house")
                        }
                
                ListPageView()
                    .tabItem {
                        Label("List", systemImage: "list.bullet")
                    }
            }
        }
    }
}

But my problem is not matter what I try , I cannot seem to show Navigation Title inside. the MyAdsPageView

struct MyAdsPageView: View {
   
    @ObservedObject var dataAd: DataController
   
    var body: some View {
       
            NavigationStack {
                
                    List {
                    }
                    .navigationTitle("My List")
    }
}

CodePudding user response:

Make navigation inside each tab view. Your code will create a nested NavigationView and app will confuse to set the title for it. And it is good to have tags for each tab. use it in this way:

struct FirstView: View {
@ObservedObject var dataController: DataController
var body: some View {
    TabView {
        
        MyAdsPageView(dataAd: dataController)
            .tabItem {
                Label("Home", systemImage: "house")
            }
            .tag(0)
        
        ListPageView()
            .tabItem {
                Label("List", systemImage: "list.bullet")
            }
            .tag(1)
    }
    
  }
}

  struct ListPageView: View {
        var body: some View {
            NavigationStack {
                ZStack {
                    
                }
                .navigationTitle("My List")
            }
        }
    }


 struct MyAdsPageView: View {
        var body: some View {
            NavigationStack {
                ZStack {
                    
                }
                .navigationTitle("My Ads")
            }
        }
    }
  • Related