Hiding the tabbar and removing the space

前端 未结 14 1699
生来不讨喜
生来不讨喜 2020-11-30 00:10

Is there a way to hide tabbar and remove that space left (around 50px) ?

I tried

self.tabBarController?.tabBar.hidden = true
self.extendedLayoutIncl         


        
14条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 01:01

    This code works on iOS 10, 11, and iPhone X (including simulators) to show/hide the tabBar. I created it several years (iOS 7 time frame?) and it has worked reliably since that time.

    It works great on iPhone X as long as content content in your childViewControllers (in tabs) is pinned to topLayoutGuide, bottomLayoutGuide or SafeArea and not the main views walls. Then it all just works. Enjoy!

    @interface UITabBarController (HideTabBar)
    @property (nonatomic, getter=isTabBarHidden) BOOL tabBarHidden;
    -(void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated;
    @end
    
    @implementation UITabBarController (HideTabBar)
    -(BOOL)isTabBarHidden
    {
        CGRect viewFrame = self.view.frame;
        CGRect tabBarFrame = self.tabBar.frame;
        return tabBarFrame.origin.y >= viewFrame.size.height;
    }
    
    -(void)setTabBarHidden:(BOOL)hidden
    {
        [self setTabBarHidden:hidden animated:NO];
    }
    
    -(void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated
    {
        BOOL isHidden = self.tabBarHidden;    
        if(hidden == isHidden)return;
    
        UIView *transitionView = [[[self.view.subviews reverseObjectEnumerator] allObjects] lastObject];
        if(transitionView == nil) {
            NSLog(@"UITabBarCategory can't get the container view");
            return;
        }    
        CGRect viewFrame = self.view.bounds;
        CGRect tabBarFrame = self.tabBar.frame;
        CGRect containerFrame = transitionView.frame;
        CGRect selectedVCFrame = containerFrame;
    
        tabBarFrame.origin.y = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height);
        containerFrame.size.height = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height);
        if([self.moreNavigationController.viewControllers containsObject:self.selectedViewController]) {
            selectedVCFrame = self.selectedViewController.view.frame;
            selectedVCFrame.size.height += hidden ? tabBarFrame.size.height : -tabBarFrame.size.height;
        }
        self.selectedViewController.view.frame = selectedVCFrame;
    
        [UIView animateWithDuration:.5 animations:^{
            self.tabBar.frame = tabBarFrame;
            transitionView.frame = containerFrame;
            [self.selectedViewController.view setNeedsLayout];
        }];
    }
    @end
    

    Usage - I call it in the viewController on rotation events like so:

    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    
        // Hide TabBar on iPhone, iPod Touch
        if([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPad) {
            if(_startDateEditor.editing) return;
            if(fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || fromInterfaceOrientation == UIInterfaceOrientationPortrait)
                [self.tabBarController setTabBarHidden:YES animated:YES];
            else
                [self.tabBarController setTabBarHidden:NO animated:YES];
        }
    }
    

提交回复
热议问题