How to hide NavigationView Bar in SwiftUI
Asked Answered
A

2

21

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?

Atrip answered 1/8, 2019 at 13:20 Comment(0)
E
55

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

Ellerd answered 1/8, 2019 at 13:29 Comment(8)
Weird, that we need to set a title to hide it... But works, thank you.Enshroud
For me it worked just by adding edgesIgnoringSafeArea Urbanite
Does it work on device rotation change. If i rotate p > l > p then there space appear i think it is related to geometry reader + navigation viewOvermaster
only .edgesIgnoringSafeArea([.top, .bottom]) work for meLuwian
In iOS 14, using .navigationBarHidden(true) is all I needed to make this work.Egghead
It worked BUT for some reason the view got moved also horizontally, so in case someone had the same issue, I fixed it by adding .position(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2)Tithonus
Not working in iOS 15Prytaneum
.navigationBarHidden(true) is enough for iOS 15.5Slowwitted
D
2

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
      }
  }
}
Darees answered 3/8, 2023 at 3:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.