0
votes

I'm trying to hide a UITabBarController and UINavigationController simultaneously when a button is touch. I found a very nice code snippet here How to hide uitabbarcontroller but I have problem when trying to hide and animate both UINavigationController and the tabbarcontroller. I also found a lot of examples on the internet when they hide the tabbar using self.tabBarController.tabBar.hidden = YES but that only hides the button items not the black bar at the bottom.

After playing a lot around I can get to make both animate correctly because I think that it's related to the hiding of the Navigation Controller which makes the size of the whole window to change on the fly.

-(IBAction)touchImage:(id)sender {

    if (isImageFullScreen) {

        isImageFullScreen = NO;

        [self.navigationController setNavigationBarHidden:NO animated:YES];
        [UIView transitionWithView:self.view
                          duration:0.5
                           options:UIViewAnimationOptionCurveLinear
                        animations:^
         {
             hotelImageButton.frame = CGRectMake(0,20,320,92);
             [self showTabBar:self.tabBarController];
         }
                        completion:^(BOOL finished)
         {
         }];

    } else {

        isImageFullScreen = YES;

        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [UIView transitionWithView:self.view
                          duration:0.5
                           options:UIViewAnimationOptionCurveLinear
                        animations:^
         {
             hotelImageButton.frame = CGRectMake(0,0,320,480);
             [self hideTabBar:self.tabBarController];
         }
                        completion:^(BOOL finished)
         {                                  
         }];
    }

}

The hideTabBar and showTabBar methods are the ones from the other post I linked above.

I also tried some other combinations but I can't make it look good. Any ideas?

Thanks in advance.

2

2 Answers

5
votes

I tried that code now and I see that the UITabBar show animation doesn't take place smoothly. I managed to make it smoother by adjusting the duration period for the tabbar showing animation to be lower.

[UIView setAnimationDuration:0.2];

Hopefully that works.

EDIT: Please try this code, it resizes the parent view to be bigger in 1 animation transaction in such a way that only the bars are hidden and the content is shown.

- (IBAction)TestButton1:(UIButton *)sender {

if(!isAnimating){
    if(isTabBarAndNavBarHidden){

        [UIView transitionWithView:self.view
                          duration:0.5
                           options:UIViewAnimationOptionTransitionNone
                        animations:^
         {
             isAnimating=YES;

             CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
             CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;

             [self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, 0, self.tabBarController.view.frame.size.width, screen_height)];
             [self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, statusBar_height, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
         }
                        completion:^(BOOL finished)
         {
             isTabBarAndNavBarHidden=NO;
             isAnimating=NO;
         }];

    }else{

        [UIView transitionWithView:self.view
                          duration:0.5
                           options:UIViewAnimationOptionTransitionNone
                        animations:^
         {
             isAnimating=YES;

             CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
             CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;

             [self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, statusBar_height-self.navigationController.navigationBar.frame.size.height, self.tabBarController.view.frame.size.width, screen_height+self.navigationController.navigationBar.frame.size.height+self.tabBarController.tabBar.frame.size.height-statusBar_height)];
             [self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];


         }
                        completion:^(BOOL finished)
         {
             isTabBarAndNavBarHidden=YES;
             isAnimating=NO;
         }];

    }
}

}

0
votes

This code is for iPhone 4/4S.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.lastContentOffset > scrollView.contentOffset.y)
    {
          NSLog(@"Scrolling up");

        [UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{



            [self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
             [self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];

             } completion:
           ^(BOOL finished) {

                [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{


                      } completion:^(BOOL finished) {
                              //
                        }];

            }];

    }
    else if (self.lastContentOffset < scrollView.contentOffset.y)
    {
        [UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
               [self.navigationController.navigationBar setFrame:CGRectMake(0, -60, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
            [self.tabBarController.tabBar setFrame:CGRectMake(0, 480, 320, 50)];


        } completion:
         ^(BOOL finished) {

             [UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

             } completion:^(BOOL finished) {

             }];

         }];



        NSLog(@"Scrolling Down");

    }

    self.lastContentOffset = scrollView.contentOffset.y;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
     [self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];



    // Do any additional setup after loading the view.
}