1

I'm trying to learn SwiftUI and I'm wondering its possible to have both Navigation bar and a Tabbar in the same project?

I currently have a tabbar but I need to to have a Navigation bar with some buttons and a logo in the middle.

How do I achieve this?

This is my current code:

struct ContentView: View {
    var body: some View {
        
        TabView {
             NavigationView{
                 FirstView()
             }
             .tabItem {
                 VStack{
                    Image(systemName: "house.fill")
                    Text("Home")
                 }
             }
             
             NavigationView{
                 SecondView()
             }
             .tabItem {
                 VStack{
                     Image("second")
                     Text("Second")
                 }
             }
         }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
xdeleon
  • 769
  • 8
  • 20
drago
  • 1,148
  • 2
  • 16
  • 35

2 Answers2

3

Use the following component navigationBarItems, navigationBarTitle, toolbar and ToolbarItem

struct ContentViewTab: View {
    var body: some View {
        
        TabView {
            NavigationView{
                Color.red
                    .navigationBarTitle("Home", displayMode: .inline)
                    .toolbar {
                        ToolbarItem(placement: .principal) {
                            Image(systemName: "star.fill")
                        }
                    }
                    .navigationBarItems(leading: Button("Left") {}, trailing: Button("Right") {})
            }
            .tabItem {
                VStack{
                    Image(systemName: "house.fill")
                    Text("Home")
                }
            }
            
            NavigationView{
                Color.green
                    //                    .item
                    .navigationBarTitle("Second", displayMode: .inline)
            }
            .tabItem {
                VStack{
                    Image("second")
                    Text("Second")
                }
            }
        }
        
    }
}

enter image description here

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
1

Here is a possible solution. Tested with Xcode 12.4 / iOS 14.4

demo

struct ContentView: View {
    var body: some View {
        TabView {
             NavigationView{
                 FirstView()
                   .navigationBarTitle("First", displayMode: .inline)  // << !!
             }
             .tabItem {
                 VStack{
                    Image(systemName: "house.fill")
                    Text("Home")
                 }
             }
             
             NavigationView{
                 SecondView()
                   .navigationBarTitle("Second", displayMode: .inline) // << !!
             }
             .tabItem {
                 VStack{
                     Image("second")
                     Text("Second")
                 }
             }
         }
        
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690