Search code examples
ios11smooth-scrollingcollectionviewsearchbar

How to hide navigationBar when scrolling collectionView in ios 11?


In iOS 11 searchBar hides when scrolling tableView if we set to hide. how can I hide searchBar, navigationBar and tabBar when scrolling collectionView up ? And unhide them all when scrolling down ? Thanks for your help...


Solution

    1. Subclass UIScrollViewDelegate in your UIViewController (i.e. class ViewController: UIViewController, UIScrollViewDelegate { codes... })
    2. Implement scrollViewDidScroll Delegate method

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