Why The Navigation Title doesn't show up using SwiftUI?
.navigationBarTitle()
and .navigationBarItem()
are modifiers on the View
inside of the NavigationView
, not on the NavigationView
itself:
struct LandmarkList: View {
var body: some View {
NavigationView {
List(landmarkData) { landmark in
LandmarkRow(landmark: landmark)
}
.navigationBarItem(title: Text("Done"))
.navigationBarTitle(Text("Landmarks"))
}
}
}
and if you think about it, this makes sense. As the View
within the NavigationView
changes, the new View
dictates what the title and contents of the navigation bar should be.
Description:
NavigationView
is just a container around some content. Contents are changing when you navigating from page to page, but the NavigationView
persists.
The point is that NavigationView
shows each view
's content when it shows it. So it will encapsulate with the destination.
Finally, you should add all navigation modifiers inside the navigation (on the content)
Example:
struct Content: View {
var body: some View {
Text("Some. content")
.navigationBarItem(title: Text("Done"))
.navigationBarTitle(Text("Landmarks"))
}
}
struct Parent: View {
var body: some View {
NavigationView {
Content() // This contains navigation modifiers inside the view itself
}
}
}