swiftUI - tabBar & navigationBar in the same view

Viewed 34

HOW DO I MAKE THEM BOTH APPEAR IN THE VIEW?

I'm trying to make a view with both tabbar & navigationbar,

but it's either shows the navigationBar or the tabBar (depends on which I put first \ on top)

for example - this will show only the Navigation Bar:

var body: some View {
        
    TabView(selection: $selectedTab) {
        
        NavigationView{
        
            CustomTableView(lang: $lang)
                .tag(0)
                .tabItem {
                    Text("Home")
                    Image(systemName: "house.fill")
                }

            
        //NavigationBar Title:
        .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    ToolbarItem(placement: .principal) {
                        let title = "Title"
                        Text(title)
                                .font(.title)
                    }
                    
                    ToolbarItem(placement: .navigationBarTrailing) {
                            Image(Constants.logoImage).resizable()
                                .scaledToFit()
                                .frame(width: 100, height: 50, alignment: .trailing)
                        }
                    }
        }
    }
}
1 Answers

The TabView interpret NavigationView as just a first page view w/o tab item, so nothing is shown. Here is possible fix:

TabView(selection: $selectedTab) {
    
    NavigationView {
    
        CustomTableView(lang: $lang)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .principal) {
                    let title = "Title"
                    Text(title)
                            .font(.title)
                }
                
                ToolbarItem(placement: .navigationBarTrailing) {
                        Image(Constants.logoImage).resizable()
                            .scaledToFit()
                            .frame(width: 100, height: 50, alignment: .trailing)
                    }
                }
    }
    .tag(0)        // << place here !!
    .tabItem {
        Text("Home")
        Image(systemName: "house.fill")
    }
}
Related