SwiftUI view is in the middle instead of in the top - NavigationView

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. I also tried with a Stack and Spacer() -still the same. I think the problem is that I am using NavigationView multiple times in the code, but I am not sure how to fix it. 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
    }
}

}

Answered by MobileTen in 708612022

HomeScreen & SignInView will inherit the outer most NavigationView, no need to nest the NavigationView them.

struct ContentView: View {
@EnvironmentObject var viewModel: AppViewModel

var body: some View {
    NavigationView {
        if viewModel.signedIn {
             HomeScreen()
              .navigationBarHidden(true)
        }
        else {
            SignInView()
               .navigationBarHidden(false)
              .navigationBarTitle("Sign In", displayMode: .inline)
        }
    }
   .onAppear {
        viewModel.signedIn = viewModel.isSignedIn 
    }
  }
}
Accepted Answer

HomeScreen & SignInView will inherit the outer most NavigationView, no need to nest the NavigationView them.

struct ContentView: View {
@EnvironmentObject var viewModel: AppViewModel

var body: some View {
    NavigationView {
        if viewModel.signedIn {
             HomeScreen()
              .navigationBarHidden(true)
        }
        else {
            SignInView()
               .navigationBarHidden(false)
              .navigationBarTitle("Sign In", displayMode: .inline)
        }
    }
   .onAppear {
        viewModel.signedIn = viewModel.isSignedIn 
    }
  }
}
SwiftUI view is in the middle instead of in the top - NavigationView
 
 
Q