5

Hiding the navigation bar on scroll was supported in Swift with navigationController?.hidesBarsOnSwipe = true

To be clear, I'd like it to only be hidden on scroll, so .navigationBarHidden(true) would not suffice.

I tried accessing the NavigationController as described in this Stackoverflow answer, (I added nc.hidesBarsOnSwipe = true) and while it compiled, it did not work.

Is this supported in SwiftUI?

blub
  • 382
  • 4
  • 16

1 Answers1

2

NavigationView seems to be relatively buggy still. For example, by default a ScrollView will ignore the title area and just scroll beneath it.

It looks to me like you can get this working by using displayMode: .inline and StackNavigationViewStyle() together.

struct ContentView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                ForEach(0...20, id: \.self) { count in
                    (count % 2 == 0 ? Color.red : Color.blue)
                        .frame(height: 44.0)
                }
            }
            .background(NavigationConfigurator { nc in // NavigationConfigurator is from the OP's post: https://stackoverflow.com/a/58427754/7834914
                nc.hidesBarsOnSwipe = true
            })
            .navigationBarTitle("Hello World", displayMode: .inline)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

Before Scroll: Before Scroll

After Scroll: After Scroll

arsenius
  • 10,530
  • 4
  • 52
  • 69