0

The screen is working perfectly fine (No navigation bar) in:

  • iPhone portrait mode
  • iPad portrait mode
  • iPad landscape mode

It is only showing navigation bar (out of nowhere) in iPhone landscape mode.

Can anyone please suggest what am I doing wrong or what can be done to disappear the navigation bar even in iPhone landscape mode? Thanks in advance.

Attaching screenshots of both portrait and landscape mode here.

Navigation bar appearing in landscape mode

Working fine (no navigation bar) in portrait mode

struct LoginView: View {

@ObservedObject var vm = LoginVM()

var body: some View {
    NavigationView {
        VStack {
                TextField("Username", text: $vm.username)
                    .padding()
                    .overlay(RoundedRectangle(cornerRadius: 10)
                    .stroke(Color.blue, lineWidth: 0.5))
                    .padding([.top, .bottom])
                
                SecureField("Password / SMS Code", text: $vm.password)
                    .padding()
                    .overlay(RoundedRectangle(cornerRadius: 10)
                    .stroke(Color.blue, lineWidth: 0.5))
                    .padding(.bottom, 20)
                
                HStack {
                    Spacer()
                    NavigationLink(
                        destination: ForgotPassView(),
                        label: {
                            Text("Forgot Password ?")
                    }).padding(.bottom, 40)
            }
        }.padding()
    }
    .navigationViewStyle(StackNavigationViewStyle())
}

}

Vinay
  • 3
  • 2

2 Answers2

1

Add .navigationBarHidden(true) to the child view of NavigationView:

var body: some View {
        NavigationView {
            VStack {
                TextField("Username", text: $vm.username)
                    .padding()
                    .overlay(RoundedRectangle(cornerRadius: 10)
                                .stroke(Color.blue, lineWidth: 0.5))
                    .padding([.top, .bottom])
                
                SecureField("Password / SMS Code", text: $vm.password)
                    .padding()
                    .overlay(RoundedRectangle(cornerRadius: 10)
                                .stroke(Color.blue, lineWidth: 0.5))
                    .padding(.bottom, 20)
                
                HStack {
                    Spacer()
                    NavigationLink(
                        destination: ForgotPassView(),
                        label: {
                            Text("Forgot Password ?")
                        }).padding(.bottom, 40)
                }
            }
            .padding()
            .navigationBarHidden(true) //<-- here
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • Adding .navigationBarHidden(true) is working fine on this specific screen but when I go to ForgotPassView() by tapping on Forgot Password? (as mentioned in the above code), then the Navigation bar of ForgotPassView() appears in white color for : - iPhone portrait mode - iPad portrait mode - iPad landscape mode and in native color i.e. grey (translucent) for iPhone landscape mode. What can be done to behave it in the same way in iPhone and iPad for both the modes ? – Vinay Mar 25 '21 at 02:21
  • I'm not sure off the top of my head, although I can experiment. Sounds like a separate question. This question was how to hide the navigation bar, which I demonstrated in this answer, correct? Or, perhaps your question is how to hide the navigation bar on subsequent views -- you would have to use the `.navigationBarHidden` again – jnpdx Mar 25 '21 at 02:25
  • I think it is interconnected, that why even I need to hide the navigation bar separately (for landscape) when it is not visible in portrait mode. Therefore, hiding nav bar using .navigationBarHidden(true) just for the sake for not showing in landscape does not looks like a perfect solution. May be my problem is a bug in SwiftUI and hiding nav bar this way does not looks like a good workaround. – Vinay Mar 25 '21 at 07:13
  • Honestly, it sounds a little like maybe you don’t want to be using NavigationView if you don’t want to show the nav bar. – jnpdx Mar 25 '21 at 13:17
0

Thanks, just:

.navigationViewStyle(StackNavigationViewStyle())

works for me!