2

I started to use SwiftUI after a couple years of UIKit.. This is not a piece of cake lol.

Alright, so I am trying to build an app that has a tab bar with 2 elements. Each Tab with contain a ViewController (View now) and they will be embedded in a NavigationController (NavigationView now)

The actual result is this enter image description here

and I am expecting to have a nav bar with a title set to Home.

Could you explain me what I do wrong here? i followed the documentation and a couple tutorials, and I don't seem to do differently.

import SwiftUI

struct TabBarView: View {
    var body: some View {
        TabView() {
            RedView()
                .tabItem({
                    Image(systemName: "house.fill")
                    Text("Home")
                })
                .tag(0)
            BlueView()
                .tabItem({
                    Image(systemName: "dollarsign.square.fill")
                    Text("Trade")
                })
                .tag(1)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        TabBarView()
    }
}


struct RedView: View {
    var body: some View {
        NavigationView {
            List {
                Text("test")
            }
        }
        .navigationBarTitle("Home")
    }
}
struct BlueView: View {
    var body: some View {
        NavigationView {
            List {
                Text("test2")
            }
        }
        .navigationBarTitle("Trade")
    }
}

This is the file that contains everything at the moment. Thanks in advance for any future help!

Oleg G.
  • 550
  • 5
  • 25

1 Answers1

4

The .navigationBarTitle should be inside NavigationView

struct RedView: View {
    var body: some View {
        NavigationView {
            List {
                Text("test")
            }
            .navigationBarTitle("Home")     // << here !!
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Wow, I am amazed by this rookie mistake! thank you! – Oleg G. Jul 16 '20 at 16:50
  • Not related, but, by any change, do you know why on an iPad the view is like this and not taking the full screen, i don't recall using a SplitViewController here. https://i.imgur.com/H299eA8.png – Oleg G. Jul 16 '20 at 16:56
  • got it with this: `.navigationViewStyle(StackNavigationViewStyle())`. Thanks again! – Oleg G. Jul 16 '20 at 17:00