Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide show top view when scroll in swift

Tags:

ios

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.

like image 244
Happiehappie Avatar asked Nov 04 '16 04:11

Happiehappie


People also ask

How do I hide Show view when scrolling up in Swift?

What you need to do is lock the view from animating once an animation is in progress and open it up once it is completed.

What is scroll view in Xcode?

The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.


2 Answers

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)
            }
        }
like image 163
Usman Nisar Avatar answered Oct 16 '22 23:10

Usman Nisar


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

  • HidingNavigationBar
  • AMScrollingNavbar

Another way is to use this function in your viewWillAppear

if let navigationController = self.navigationController as? ScrollingNavigationController {
    navigationController.followScrollView(tableView, delay: 30.0)
}
like image 42
Munahil Avatar answered Oct 16 '22 23:10

Munahil