0

I am trying to create a navigation view in SwiftUI - I want to navigate from the login form to the Home Screen. In the preview, the Home Screen is looking how it should, but on the live preview - it is centered in the middle of the screen. I tried with .navigationBarHidden(true), it goes a little bit up, but still not on the top of the screen. Please help.

struct ContentView: View {

    @EnvironmentObject var viewModel: AppViewModel
    
    var body: some View {
        NavigationView {
            if viewModel.signedIn {
                NavigationView {
                    HomeScreen()
                }
                .navigationBarHidden(true)
            }
            else {
                SignInView()
            }

        }
        .onAppear {
            viewModel.signedIn = viewModel.isSignedIn
        }
    }

}
vadian
  • 274,689
  • 30
  • 353
  • 361

2 Answers2

1

You need to "push" the view up by using a Spacer() inside a VStack:

var body: some View {
    NavigationView {

      // Add this
      VStack {
        if viewModel.signedIn {
            NavigationView {
                HomeScreen()
            }
            .navigationBarHidden(true)
        }
        else {
            SignInView()
        }

        // This will push the view up
        Spacer()
      }
    }
    .onAppear {
        viewModel.signedIn = viewModel.isSignedIn
    }
}
HunterLion
  • 3,496
  • 1
  • 6
  • 18
0

Add a VStack and Spacer. The spacer consumes the rest of the space below the HomeScreen

NavigationView {
    VStack {
       HomeScreen()
       Spacer()
    }
}

If SignInView is also supposed to be on top move the VStack and the Spacer up accordingly.

vadian
  • 274,689
  • 30
  • 353
  • 361