Search code examples
uinavigationcontrollerswiftuinavigationview

Hide navigation bar Swiftui


I've a problem with the NavigationView in SwiftUI, I've add this code for hide the navigation and works fine but in when scroll the view appear the sticky header how to remove this? thanks!!

NavigationView {
...
}
.navigationBarTitle("")
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)

enter image description here


Solution

  • Moving the navigationBarTitle and navigationBarHidden within the NavigationView will remove the sticky header. Here's the code.

    import SwiftUI
    
    struct ContentView: View {
        let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    
        var body: some View {
            NavigationView {
                List(months, id: \.self) { month in
                    NavigationLink(destination: DetailView(month: month)) {
                            Text(month)
                    }
                }
                .navigationBarTitle("")
                .navigationBarHidden(true)
            }
            //.navigationBarBackButtonHidden(true)
        }
    }
    
    struct DetailView: View {
        let month: String
        var body: some View {
            Text(month)
        }
    }