RussellHarrower
RussellHarrower

Reputation: 6820

Navigation Title not showing

So at first I wanted to get rid of the Navigation bar from the home screen as it does not want to show the title & I don't really need it.

However I can't remove it because I use NavigationLink. So I am left in a little pickle.

How do I either hide the toolbar/navigation bar on home page or show the title.

MIN CODE

struct ContentView: View {
    

   var body: some View {
        NavigationView {
          
                Text("Loading")
          
        }.navigationBarTitle(Text("Home"))
    }
}

So after a few more attempts seems that the title had to be shown in

  var body: some View {
        NavigationView {
            
            if posts.isEmpty {
                Text("Loading")
                //.navigationBarTitle("") //this must be empty
                .navigationBarHidden(true)
                      
            } else {
                  (REST OF CODE)
            }
    }
   }

Still can't hide it though. The issue is it keeps disappearing when the if statement runs.

Upvotes: 1

Views: 761

Answers (1)

Asperi
Asperi

Reputation: 258591

The .navigationBarTitle, as well as other similar modifiers, should be used inside NavigationView, like

struct ContentView: View {
   var body: some View {
        NavigationView {
           Text("Loading")
            .navigationBarTitle(Text("Home"))    // << here !!
        }
    }
}

Update: if you want to make modifier work for both condition branches wrap condition into some container (VStack, Group, etc.), like

var body: some View {
   NavigationView {
      Group {         
        if posts.isEmpty {
            Text("Loading")
        } else {
              (REST OF CODE)
        }
      }
      //.navigationBarTitle("") //this must be empty
      .navigationBarHidden(true)    // << here !!
   }
}

Upvotes: 2

Related Questions