Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the navigation bar and toolbar as scroll down? Swift (like myBridge app)

I want to hide a toolbar and nav bar as I scroll down a page. And return it as I scroll up. How is this possible?

How would I go about detecting the drag? Do I use pan gesture or is this down with the scrollview?

like image 483
Lyndon King McKay Avatar asked Nov 18 '16 01:11

Lyndon King McKay


People also ask

How do I hide navigation bar in Swift storyboard?

Click on the controller that has the top bar navigate to the properties bar on the right hand side of Xcode. There is a drop down labeled Top Bar (as shown above) change this drop down to none.

How do I hide and show navigation bar?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

How do I hide the top navigation bar in Swift?

To hide the navigation bar in Swift, you'll need to add code to two methods: viewWillAppear and viewWillDisappear . That's it to hide the navigation bar in your view controller.


2 Answers

Try this simple approach: Tested in Swift 3

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {      if(velocity.y>0) {         //Code will work without the animation block.I am using animation block incase if you want to set any delay to it.         UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: {              self.navigationController?.setNavigationBarHidden(true, animated: true)              self.navigationController?.setToolbarHidden(true, animated: true)             print("Hide")         }, completion: nil)      } else {         UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: {              self.navigationController?.setNavigationBarHidden(false, animated: true)             self.navigationController?.setToolbarHidden(false, animated: true)             print("Unhide")         }, completion: nil)           }    } 

Output: Updated

enter image description here

Note: If you passing any data from this VC to another VC that embedded with navigationController.You may need to unhide the NavigationBar.

like image 170
Joe Avatar answered Sep 18 '22 20:09

Joe


Easily to do this:

navigationController?.hidesBarsOnSwipe = true 
like image 32
Huu Phong Nguyen Avatar answered Sep 20 '22 20:09

Huu Phong Nguyen