在 ios 11 中滚动 collectionView 时如何隐藏导航栏?

Bat*_*Can 1 smooth-scrolling collectionview searchbar ios11

在 iOS 11 中,如果我们设置为隐藏,则在滚动 tableView 时 searchBar 会隐藏。向上滚动 collectionView 时如何隐藏 searchBar、navigationBar 和 tabBar?并在向下滚动时取消隐藏它们?谢谢你的帮助...

小智 5

  1. 在您的 UIViewController 子类 UIScrollViewDelegate (即class ViewController: UIViewController, UIScrollViewDelegate { codes... }
  2. 实现 scrollViewDidScroll 委托方法

    func scrollViewDidScroll(scrollView: UIScrollView) { 
        let pan = scrollView.panGestureRecognizer
        let velocity = pan.velocityInView(scrollView).y
        if velocity < -5 { 
            self.navigationController?.setNavigationBarHidden(true, animated: true) 
            self.navigationController?.setToolbarHidden(true, animated: true)
        } else if velocity > 5 {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
            self.navigationController?.setToolbarHidden(false, animated: true)
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)