Jason Tremain
Jason Tremain

Reputation: 1399

SwiftUI Navigation Bar Title doesn't appear

I'm not sure if anything changed in Beta 3, however, when trying to add the NavigationBarTitle modifier to NavigationView, it does not show the text for the title? Any ideas?

NavigationView {
         List(0 ..< 20) { item in
            NavigationLink(destination: Text("1")) {
                Text("Navigate 1")
            }
        }
    }.navigationBarTitle(Text("Update")).navigationBarHidden(false)
}

The list shows but no title for the list in the NavigationView

Upvotes: 7

Views: 6195

Answers (2)

Eldar
Eldar

Reputation: 555

Your code works fine and the navigationBarTitle is not outdated. It must be placed above (inside the Navigation View). Yes, it is sometimes confusing, it is necessary to remember this.

To the place where you currently have it .navigationBarTitle(Text ("Update")).navigationBarHidden(false) you need to set the modifier .navigationViewStyle(StackNavigationViewStyle ()), which means that you should always show the first screen regardless of the screen size.

    var body: some View {
        NavigationView {
                 List(0 ..< 20) { item in
                    NavigationLink(destination: Text("1")) {
                        Text("Navigate 1")
                    }
                }
                 .navigationBarTitle(Text("Update"), displayMode: .automatic).navigationBarHidden(false)
            }
        // that means only show one view at a time no matter what device I'm working
        .navigationViewStyle(StackNavigationViewStyle())
        }

Upvotes: 0

graycampbell
graycampbell

Reputation: 7800

You're setting .navigationBarTitle and .navigationBarHidden on NavigationView when they should be modifiers on List instead:

NavigationView {
    List(0..<20) { item in
        NavigationLink(destination: Text("1")) {
            Text("Navigate 1")
        }
    }
    .navigationBarTitle("Update")
    .navigationBarHidden(false)
}

You can also just remove .navigationBarHidden(false) (unless you're setting it to true in a previous view or something).

Upvotes: 24

Related Questions