Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Navigation Bar Title doesn't appear

Tags:

swiftui

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

like image 722
Jason Tremain Avatar asked Jul 15 '19 01:07

Jason Tremain


People also ask

How do I add a navigation title in SwiftUI?

To customize a navigation bar title view in SwiftUI, we simply set ToolbarItem of placement type . principal to a new toolbar modifier. Text("Hello, SwiftUI!") <1> Because this is a customize of navigation bar title, a view needs to be embedded inside a NavigationView .

How do I change the navigation bar title color in SwiftUI?

To change a navigation bar color in SwiftUI, you apply toolbarBackground modifier to the content view of NavigationStack . NavigationView is deprecated in iOS 16. toolbarBackground accepts two parameters. ShapeStyle : The style to display as the background of the bar.

How do I use navigation in SwiftUI?

Before NavigationStack and NavigationSplitView , SwiftUI introduced the NavigationView and NavigationLink structs. These two pieces work together to allow navigation between views in your application. The NavigationView is used to wrap the content of your views, setting them up for subsequent navigation.


1 Answers

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).

like image 167
graycampbell Avatar answered Oct 12 '22 11:10

graycampbell