This is the code that I actually use in a production application.
It is located in Swift , and also updates UITabBar.hidden var.
func scrollViewWillBeginDragging(scrollView: UIScrollView) { if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{ changeTabBar(hidden: true, animated: true) } else{ changeTabBar(hidden: false, animated: true) } }
You can also use another callback method:
func scrollViewDidScroll(scrollView: UIScrollView) { ... }
but if you choose this, then you must handle several calls to the helper method, which actually hides the tabBar.
And then you need to add this method, which animates the tabBar hiding / showing.
func changeTabBar(hidden:Bool, animated: Bool){ var tabBar = self.tabBarController?.tabBar if tabBar!.hidden == hidden{ return } let frame = tabBar?.frame let offset = (hidden ? (frame?.size.height)! : -(frame?.size.height)!) let duration:NSTimeInterval = (animated ? 0.5 : 0.0) tabBar?.hidden = false if frame != nil { UIView.animateWithDuration(duration, animations: {tabBar!.frame = CGRectOffset(frame!, 0, offset)}, completion: { println($0) if $0 {tabBar?.hidden = hidden} }) } }
Update Swift 4
func changeTabBar(hidden:Bool, animated: Bool){ guard let tabBar = self.tabBarController?.tabBar else { return; } if tabBar.isHidden == hidden{ return } let frame = tabBar.frame let offset = hidden ? frame.size.height : -frame.size.height let duration:TimeInterval = (animated ? 0.5 : 0.0) tabBar.isHidden = false UIView.animate(withDuration: duration, animations: { tabBar.frame = frame.offsetBy(dx: 0, dy: offset) }, completion: { (true) in tabBar.isHidden = hidden }) }
Ariel Hernández Amador
source share