Hiding the tabbar and removing the space

2020-01-27 02:12发布

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

I tried

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

No luck. I see blank space.

14条回答
叛逆
2楼-- · 2020-01-27 03:10

Sometimes that easiest way is just to add a view that uses the UIScreen bounds.

let whiteView = UIView()
    whiteView.backgroundColor = .white
    view.addSubview(whiteView)
    whiteView.translatesAutoresizingMaskIntoConstraints = false
    whiteView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    whiteView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    whiteView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    whiteView.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height).isActive = true

Cause sometimes the view edges extends beyond the nav bar giving you new problems if you extend the view layout.

查看更多
一纸荒年 Trace。
3楼-- · 2020-01-27 03:13

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];
    }
}
查看更多
登录 后发表回答