3

How to to hide the navigation bar when swiping up and to show when swiping down (like on facebook for example) in SwiftUI? In UKit there is navigationBar.hideBarsOnSwipe, but I do cannot seem to find such functionality in SwiftUI. Am I missing something, or there is indeed no hide on swipe in swiftUI?

Thanks in advance!!

  • SwiftUI is still very young then, and many features are not supported. I can't find anything related t this in [Apple's documentation](https://developer.apple.com/documentation/swiftui/navigationview-view-modifiers). Maybe integrating with UIKit will be an acceptable choice. – xiaoyu2006 Jul 04 '20 at 12:31

2 Answers2

3

No native API in SwiftUI so far (both 1.0 & 2.0). So here is a possible working solution based on NavigationConfigurator provided in this answer

Tested with Xcode 12 / iOS 14

Update: retested with Xcode 13.4 / iOS 15.5 - still works fine!

demo

struct TestHideOnSwipe: View {

    var body: some View {
        NavigationView {
            List(0..<100) { i in
                Text("Item \(i)")
            }
            .background(NavigationConfigurator { navigationConfigurator in
                navigationConfigurator.hidesBarsOnSwipe = true     // << here !!
            })
            .navigationBarTitle(Text("Demo"), displayMode: .inline)
        }
    }
}

Test module on GitHub

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • 1
    Helpful extension: `extension View { func navigationBarsHideOnSwipe(_ hidesBarsOnSwipe: Bool) -> some View { self.background(NavigationConfigurator { $0.hidesBarsOnSwipe = hidesBarsOnSwipe }) } }` – Natanel Jul 12 '20 at 11:19
  • 1
    it does hide it correctly, but does not show it again :( – Arslan Asim Sep 28 '21 at 15:10
-3

click on the highlighted checkbox

you can get this attribute in navigation controller's attributes inspector.

  func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

if(velocity.y>0) {
    //Code will work without the animation block.I am using animation block incase if you want to set any delay to it.
    UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: { 
        self.navigationController?.setNavigationBarHidden(true, animated: true) 
        self.navigationController?.setToolbarHidden(true, animated: true)
        print("Hide")
    }, completion: nil)

} else {
    UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: { 
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        self.navigationController?.setToolbarHidden(false, animated: true)
        print("Unhide")
    }, completion: nil)    
  }

}

if you want to do it programmatically. Note: If you passing any data from this VC to another VC that embedded with navigationController.You may need to unhide the NavigationBar.