Building a watchOS app for the first time, and for some reason I’m not able to get the title to show up.
I just made a few initial changes to ContentView:
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "drop")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("some info")
.padding()
}
.navigationTitle("title")
}
}
Everything within the VStack is displayed in the preview, but no matter where I put navigationTitle, it doesn’t show up in the corner like it should. It doesn’t show up either in the previews or simulator. What am I missing?
>Solution :
It seems that you could be missing a navigation view but it’s hard to tell without seeing if you have the ContentView
inside a navigation view.
Let’s look to the docs for a sec:
navigationTitle(_:) – "…On iOS and watchOS, when a view is navigated to inside of a navigation view, that view’s title is displayed in the navigation bar…"
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
SomeOtherView()
}
.navigationTitle("title")
}
}
}
In my example above, I use NavigationView
, which you will see widely in SwiftUI project written in the past few years. However, NavigationView
has been deprecated and replaced by other navigation types. Take a look at the docs here for NavigationStack
to see if your watchOS version is compatible with the new navigation types!