Happiehappie
Happiehappie

Reputation: 1074

Hide show top view when scroll in swift

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.

Upvotes: 5

Views: 15821

Answers (3)

KKRocks
KKRocks

Reputation: 8322

Try this it is working in my project:

self.navigationController.hidesBarsOnSwipe = true;

Upvotes: 0

Usman Nisar
Usman Nisar

Reputation: 3091

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

Upvotes: 11

Munahil
Munahil

Reputation: 2419

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

Upvotes: 1

Related Questions