swiftuinavigationbaruiscrollviewdelegate

Display navigationbar when scrolling down


On the top of the ViewController, I have a image and I hide the navigationbar, for a better visual effect.

If the user scrolls up, there is a zoom on the image. No problem so far.

If the user scrolls down, I want to display the navigation bar with animation (very light to the correct background color of the navbar)

I ve checked here a good tutorial with the new possibilities with Ios8. In fact, i need to perform the opposite of hidesBarsOnSwipe

So firstly, to hide the navigationbar I need to

self.navigationController?.isNavigationBarHidden = true

And after some search, I think I will need to use UIScrollViewDelegate.

But I have no idea how could i implement it .

Any hint?


Solution

  • What you have to do is to implement the UIScrollViewDelegate and more precisely the scrollViewDidScroll(_:) method (see documentation). This method is called each time the scroll view is scrolled.

    So, in this method, you have to check that the user scrolled down and then hide the navigation bar by calling the setNavigationBarHidden(_:animated:) method of your current navigation controller (see documentation)