SwiftUI How To Hide The Navigation Bar While Keeping The Back Button
Asked Answered
M

1

7

So I'm trying to hide the navigationBar in a Details view in SwiftUI. I've technically gotten it to work by using an init() in a different view, but the issue is that it's making the navigationBar transparent for the whole app, which I only want it in one view. The reason I haven't used an init() in the DetailsView is because I have a variable that needs an input, so I wasn't sure how to do that! Here is the code for the initializer:

init() {
    let navBarAppearance = UINavigationBar.appearance()
    navBarAppearance.backgroundColor = .clear
    navBarAppearance.barTintColor = .clear
    navBarAppearance.tintColor = .black
    navBarAppearance.setBackgroundImage(UIImage(), for: .default)
    navBarAppearance.shadowImage = UIImage()
}

Here's What The Content View and Details View code is like with the init() inside the detailsView:

// ContentView //

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(0..<5) { i in
                    NavigationLink(destination: DetailsView(test: 1)) {
                        Text("DetailsView \(i)")
                    }
                }
                
            }
            .listStyle(InsetGroupedListStyle())
            .navigationBarTitle("Test App")
        }
    }
}

// DetailsView //

struct DetailsView: View {
    
    var test: Int
    
    var body: some View {
        ScrollView {
            Text("More Cool \(test)")
            Text("Cool \(test)")
            Text("Less Cool \(test)")
        }
    }
    
    init(test: Int) {
        self.test = 8
        let navBarAppearance = UINavigationBar.appearance()
        navBarAppearance.backgroundColor = .clear
        navBarAppearance.barTintColor = .clear
        navBarAppearance.tintColor = .black
        navBarAppearance.setBackgroundImage(UIImage(), for: .default)
        navBarAppearance.shadowImage = UIImage()
    }
}

struct DetailsView_Previews: PreviewProvider {
    static var previews: some View {
        DetailsView(test: 8)
    }
}

It's a heavily edited version of my code, but it shows the problem I have. With no variables needing to be passed in, the init() worked to remove the bar in only that view. However, with that variable input, not only does it change all the views to the "8" for the number, but it also doesn't even hide the navigationBar. I'm not sure if I'm just doing something wrong nor if this is even the right way to do it, but any help would be appreciated!

Also, on a side note, does anyone know how to hide the statusBar in iOS 14 with the NavigationView?

Metathesize answered 14/7, 2020 at 15:28 Comment(0)
L
9

I think you try to use UIKit logic instead of the SwiftUI one. This is what I would do to hide the navigation bar with a back button on the top leading side of your view. As for hiding the status bar, I would use .statusBar(hidden: true). But it seems not to work on iOS14. It may be a bug... You can refer yourself to the Apple documentation on this topic.

struct DetailsView: View {
  
  @Environment(\.presentationMode) var presentation
  
  var test: Int
  
  var body: some View {
    ZStack(alignment: .topLeading) {
      
      ScrollView {
        Text("More Cool \(test)")
        Text("Cool \(test)")
        Text("Less Cool \(test)")
      }
      
      Button(action: { presentation.wrappedValue.dismiss() }) {
        HStack {
          Image(systemName: "chevron.left")
            .foregroundColor(.blue)
            .imageScale(.large)
          Text("Back")
            .font(.title3)
            .foregroundColor(.blue)
        }
      }
      .padding(.leading)
      .padding(.top)
    }
    .navigationTitle(Text(""))
    .navigationBarHidden(true)
    .statusBar(hidden: true)
  }
}
Ljubljana answered 15/7, 2020 at 6:18 Comment(1)
Gotcha, I wasn't sure the best way to go about this, but this will work fine, thank you!Metathesize

© 2022 - 2024 — McMap. All rights reserved.