6

I have a custom view as my collection view header. But of course when I scroll, the header disappears until I scroll back to the top.

An example of what I want to achieve is like the current Facebook app. Where the "LIVE, Photo, Check in" view hides when you scroll down, and returns once you scroll upwards a bit.

example

It's like this. But I just want the live, photo and check in bar hidden and show while scroll.

My current approach is just add as a collection view header.

Happiehappie
  • 1,084
  • 2
  • 13
  • 26
  • It has been answered here http://stackoverflow.com/questions/28631985/how-to-hide-navigation-bar-and-tab-bar-while-scrolling-table-view-in-ios – Windindi Nov 04 '16 at 06:09
  • But I want to hide the header view. Not the navbar – Happiehappie Nov 04 '16 at 06:18
  • can you post screenshot of your problem and codes? – Joe Nov 04 '16 at 06:18
  • Check my edited question – Happiehappie Nov 04 '16 at 06:24
  • 1
    Add a gesture recognizer to handle scrolling events on collection view. Detect direction of gesture to hide/unhide the header view. – Windindi Nov 04 '16 at 06:27
  • I can imagine the direction. But how to do the hide/unhide as a collection view header – Happiehappie Nov 04 '16 at 06:32
  • 1
    I would suggest remove the header view out of the collection view and create it as separate view on top of the collection view. Now while scrolling change alpha/ translate the independent header view accordingly. – Windindi Nov 04 '16 at 12:22
  • Hey did you ever figure this out? – Munib Jul 22 '17 at 02:07
  • I am doing something very similar, I have a small view acting as a header and then I have a larger view called contentView in which I put a uicollectionview. I want the header to dissapear and appear when ever I scroll down but I am not sure how to implement this behaviour. – Munib Jul 22 '17 at 02:14

3 Answers3

11

Here is code for hiding navigation bar with scroll

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
            //
            contentOffSet = self.channelsCollView.contentOffset.y;
        }

        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            //

            let scrollPos = self.channelsCollView.contentOffset.y ;

            if(scrollPos >= contentOffSet ){
                //Fully hide your toolbar
                UIApplication.shared.isStatusBarHidden = true
                UIView.animate(withDuration: 0.5, animations: {
                    //
                    //write a code to hide
self.navigationController?.isNavigationBarHidden = true
                }, completion: nil)
            } else {
                //Slide it up incrementally, etc.
                UIApplication.shared.isStatusBarHidden = false
                UIView.animate(withDuration: 0.5, animations: {
                    //
self.navigationController?.isNavigationBarHidden = false
                }, completion: nil)
            }
        }
Usman Nisar
  • 3,031
  • 33
  • 41
1

You can use these libraries, which manages hiding and showing of Navigation bar as user scrolls :

Another way is to use this function in your viewWillAppear

if let navigationController = self.navigationController as? ScrollingNavigationController {
    navigationController.followScrollView(tableView, delay: 30.0)
}
Munahil
  • 2,381
  • 1
  • 14
  • 24
0

Try this it is working in my project:

self.navigationController.hidesBarsOnSwipe = true;
Community
  • 1
  • 1
KKRocks
  • 8,222
  • 1
  • 18
  • 84