hiding TabBar when rotating iPhone device to landscape

前端 未结 8 1590
旧时难觅i
旧时难觅i 2020-12-09 12:21

So here is what i have: A UITabBarController that handles different UIViewControllers. In one of the UIViewController i am trying to switch the view being displayed when the

相关标签:
8条回答
  • 2020-12-09 12:36

    Subclass your TabBarController and hide the TabBar when needed:

    class tabBarVC: UITabBarController {
    
        override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
            if size.height < size.width {
                self.tabBar.hidden = true
            } else {
                self.tabBar.hidden = false
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-09 12:37

    This worked for me.

    
    - (void)viewDidLoad {
        [super viewDidLoad];    
        previousRect = self.view.frame; 
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
    {
        if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {       
            [self.navigationController setNavigationBarHidden:TRUE animated:FALSE]; 
            [[UIApplication sharedApplication] setStatusBarHidden:TRUE animated:FALSE];
        }
        else
        {
            [self.navigationController setNavigationBarHidden:FALSE animated:FALSE];
            [[UIApplication sharedApplication] setStatusBarHidden:FALSE animated:FALSE];
        }
    }
    
    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
        UIInterfaceOrientation toOrientation = self.interfaceOrientation;
    
        if ( self.tabBarController.view.subviews.count >= 2 )
        {
            UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0];
            UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];
    
            if(toOrientation == UIInterfaceOrientationLandscapeLeft || toOrientation == UIInterfaceOrientationLandscapeRight) {                 
                transView.frame = CGRectMake(0, 0, 480, 320 );
                tabBar.hidden = TRUE;
            }
            else
            {               
                transView.frame = previousRect;     
                tabBar.hidden = FALSE;
            }
        }
    }
    
    
    0 讨论(0)
  • 2020-12-09 12:37

    Maybe you want to use this

    - (void)willAnimateRotationToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
        __block UIView *weakTabBar = [self.tabBarController.view.subviews objectAtIndex:1];
        weakTabBar.alpha = 0;
        [UIView animateWithDuration:duration
                              delay:0
                            options:UIViewAnimationOptionCurveEaseIn // slow at the beggining
                         animations:^{
                             weakTabBar.alpha = 1;
                         }
                         completion:^(BOOL finished) {
                             weakTabBar.alpha = 1;
                         }];
        }
    

    }

    This doesn't hide the tab bar but makes the rotate animation smoother.

    0 讨论(0)
  • 2020-12-09 12:38
    • The solution above worked for me too just some little changes for iOS6 and above:
    • in iOS6 remove the line: " previousRect = self.view.frame; "
    • also replace " animated: " with " withAnimation: "
    • and remove " transView.frame = previousRect; " from the bottom (in else function)
    • It works for me this way. And a big thanks to user UB.
    0 讨论(0)
  • 2020-12-09 12:45

    If you have your UITabBarController then put a UINavigationController inside it then you can use hidesBottomBarWhenPushed (with a bit of trickery) to do this.

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
        if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
            self.hidesBottomBarWhenPushed = NO;
            self.navigationController.viewControllers = self.navigationController.viewControllers;
            [self transitionToGridLayout];
        }
        else {
            self.hidesBottomBarWhenPushed = YES;
            self.navigationController.viewControllers = self.navigationController.viewControllers;
            [self transitionToCoverflowLayout];
        }
    }
    

    The trick is to push your view controller so that the hidesBottomBarWhenPushed flag is picked up. You can use following.

    self.navigationController.viewControllers = self.navigationController.viewControllers;
    
    0 讨论(0)
  • 2020-12-09 12:52

    This code works fine but when i dismiss a uiviewcontroller which is presented modally, my view is under the status bar by 20 pixel. My view is inside a navigationcontroller so i do not hide it before rotation.

    0 讨论(0)
提交回复
热议问题