0

I try to align content on top of page. It works but as soon as I add a NavigationView, it doesn't work anymore.

import SwiftUI
struct Stats: View {
    var body: some View {
        NavigationView {    // <- OK without navigation view
            VStack {
                Text("Stats")
                Spacer()
            }
        }
    }
}

Seems a basic problem, but didn't succeed to work it out :( Thanks

Eric
  • 592
  • 10
  • 26
  • 2
    Does this answer your question? [How to remove the default Navigation Bar space in SwiftUI NavigationView](https://stackoverflow.com/questions/57517803/how-to-remove-the-default-navigation-bar-space-in-swiftui-navigationview) – EmilioPelaez Mar 18 '22 at 09:06
  • Thanks Emilio, that was it. I did try .navigationBarHidden(true) but I applied it to the navigation view, and it must be applied to the Stack inside the navigation view ! – Eric Mar 18 '22 at 09:20

2 Answers2

2

Regards to Emilio's réponse, here is the solution.

import SwiftUI
struct Stats: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("Stats")
                Spacer()
            }
            .navigationBarHidden(true)
        }
    }
}

Take care to apply navigationBarHidden to the Stack and not NavigationView.

Eric
  • 592
  • 10
  • 26
1

You just need to hide NavigationBar.

 NavigationView {   
             VStack {
                 Text("Stats")
                 Spacer()
                               }
                     .navigationBarTitle("")
                     .navigationBarHidden(true)
             
         }