Philipp B. Philipp B. - 11 months ago 132
iOS Question

iOS/Swift - Hide/Show UITabBarController when scrolling down/up

I'm quite new to iOS development. Right now i'm trying to hide my tabbar when I scroll down and when scrolling up the tabbar should appear. I would like to have this animated in the same way like the navigation bar. For the navigation bar I simply clicked the option in the Attributes Inspector. I saw some examples for the toolbar, but I cant adopt it the tabbar.

self.tabBarController?.tabBar.hidden = true
just hides my tabbar, but its not animated like the navigation controller.

Answer Source

This is code that i'm actually using in a production app.

It's in Swift and it also updates UITabBar.hidden var.

func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translationInView(scrollView).y < 0{
        changeTabBar(true, animated: true)
        changeTabBar(false, animated: true)

You can also use the other callback method:

func scrollViewDidScroll(scrollView: UIScrollView) {

but if you choose so, then you most handle multiple calls to the helper method that actually hides the tabBar.

And then you need to add this method that animates the hide/show of the tabBar.

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
            animations: {tabBar!.frame = CGRectOffset(frame!, 0, offset)},
            completion: {
                if $0 {tabBar?.hidden = hidden}