How to hide the navigation bar and toolbar as scroll down? Swift (like myBridge app)
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
Note: If you passing any data from this VC to another VC that embedded with navigationController
.You may need to unhide
the NavigationBar
.
In my opinion the proper way to handle navigation bar in Tableview as follows. This would applicable if we have section header in Tableview.
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) { if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 { navigationController?.setNavigationBarHidden(true, animated: true) } else { navigationController?.setNavigationBarHidden(false, animated: true) }}