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!!
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!
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)
}
}
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.