Search code examples
iosanimationuitabbar

Improve tabBar animation when hidden


I've think about how to make the tabBar 's hidden animation more elegant and smoothly:

Here is how I implement:
So I just want to improve the animation, while the tabBar is suddenly, you know, disappear and hidden.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
  [self.tabBarController.tabBar setHidden:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  [self.tabBarController.tabBar setHidden:NO];
}

Any suggestion?


Solution

  • Try adding this method:

    - (void)setTabBarHidden:(BOOL)tabBarHidden animated:(BOOL)animated
    {
      if (tabBarHidden == _isTabBarHidden)
        return;
    
      CGFloat offset = tabBarHidden ? self.tabBarController.tabBar.frame.size.height : -self.tabBarController.tabBar.frame.size.height;
    
      [UIView animateWithDuration:animated ? 0.6 : 0.0
                            delay:0
           usingSpringWithDamping:0.7
            initialSpringVelocity:0.5
                          options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionLayoutSubviews
                       animations:^{
                         self.tabBarController.tabBar.center = CGPointMake(self.tabBarController.tabBar.center.x,
                                                                           self.tabBarController.tabBar.center.y + offset);
                       }
                       completion:nil];
    
      _isTabBarHidden = tabBarHidden;
    }
    

    Then you can call it like [self setTabBarHidden:YES animated:YES] and [self setTabBarHidden:NO animated:YES] to hide and show your bar, this will move it in and out of the screen instead of just make it instantly dissapear.

    Don't forget to add a new bool property isTabBarHidden and also you can play with the values of the animation.