user2971617
user2971617

Reputation: 63

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?

Upvotes: 1

Views: 1629

Answers (2)

PRakash
PRakash

Reputation: 69

  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let defaultOffset = view.safeAreaInsets.top
    let offset = scrollView.contentOffset.y + defaultOffset
    
    navigationController?.navigationBar.transform = .init(translationX: 0, y: min(0, -offset))
}

 use this function and it scrolls up the navigation bar while scrolling up and whenever you scroll down then the navigation bar appears again..

Upvotes: 0

Adrien
Adrien

Reputation: 166

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)

Upvotes: 2

Related Questions