Bohdan Savych
Bohdan Savych

Reputation: 3457

How to hide NavigationView Bar in SwiftUI

I cannot hide NavigationView bar. I tried both variants:

Code 1:

  public var body: some View {
    NavigationView {
      MasterView()
        .navigationBarHidden(true)
    }
  }

Code 2:

  public var body: some View {
    NavigationView {
      MasterView()
    }
      .navigationBarHidden(true)
  }

Does anyone have an idea how to fix it?

Upvotes: 21

Views: 26012

Answers (2)

Viet Hung Do
Viet Hung Do

Reputation: 36

navigationBarHidden will be deprecated in a future.

Solution:

struct HiddenNavUIView: View {
  @State private var tabState: Visibility = .hidden
  
  var body: some View {
      NavigationStack {
        ScrollView {
          VStack(spacing: 12) {
            ForEach(1...50, id: \.self) { index in
              Text("Row \(index)")
              .frame(height: 32)
            }
          }
          .padding(15)
        }
        .navigationTitle("Hello")
        .toolbar(tabState, for: .navigationBar) // <- here
      }
  }
}

Upvotes: 2

Giuseppe Sapienza
Giuseppe Sapienza

Reputation: 4708

Seems that the solution could be adding a title or removing the space from safe area.

The problem:

enter image description here

Solution 1:

.navigationBarHidden(true)
.navigationBarTitle(Text("Home"))

Solution 2 (this seems be the best):

.navigationBarHidden(true)
.navigationBarTitle(Text("Home"))
.edgesIgnoringSafeArea([.top, .bottom])

enter image description here

Upvotes: 57

Related Questions