在SwiftUI中,如何在向上滑动时隐藏导航栏并在向下滑动时显示(例如在Facebook上)?在UKit中,有navigationBar.hideBarsOnSwipe
,但我似乎在SwiftUI中似乎找不到这种功能。我是否丢失了某些东西,或者在swiftUI中滑动时确实没有隐藏物?
提前谢谢!
答案 0 :(得分:1)
到目前为止,SwiftUI中都没有本机API(1.0和2.0)。因此,这是基于this answer
中提供的NavigationConfigurator
的可行解决方案
通过Xcode 12 / iOS 14测试
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)
}
}
}
答案 1 :(得分:-1)
您可以在导航控制器的属性检查器中获取此属性。
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)
}
}
如果要以编程方式进行操作。 注意:如果将任何数据从该VC传递到另一个嵌入了navigationController的VC。则可能需要取消隐藏NavigationBar。