Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.navigationTitle not showing up in Previews or Simulator

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading