簡體   English   中英

在 SwiftUI 中滑動列表時隱藏導航欄

[英]Hide navigation bar on swipe of a list in SwiftUI

如何在 SwiftUI 中向上滑動時隱藏導航欄並在向下滑動時顯示(例如在 facebook 上)? 在 UKit 中有navigationBar.hideBarsOnSwipe ,但我似乎在 SwiftUI 中找不到這樣的功能。 我是否遺漏了什么,或者 swiftUI 中的滑動確實沒有隱藏?

提前致謝!!

到目前為止,SwiftUI 中沒有原生 API(1.0 和 2.0)。 因此,這是一個基於此答案中提供的NavigationConfigurator的可能工作解決方案

用 Xcode 12 / iOS 14 測試

更新:使用 Xcode 13.4 / iOS 15.5 重新測試 - 仍然可以正常工作!

演示

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)
        }
    }
}

GitHub上的測試模塊

單擊突出顯示的復選框

您可以在導航控制器的屬性檢查器中獲取此屬性。

  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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM