iOS 隐藏&显示tabBar

5,627 阅读1分钟

一般的界面显示需求:在主页面是显示 tabBar 的,在所有的子页面隐藏 tabBar,做法很简单

当push到一个新的页面时隐藏tabBar
viewController.hidesBottomBarWhenPushed = YES;

但是,需求是这样的:

主页是显示tabBar的,进入第二个页面,隐藏tabBar,再进入第三个页面,显示tabBar。

废话不多说了,看代码

// 强制显示tabbar
NSArray *views = self.tabBarController.view.subviews;
UIView *contentView = [views objectAtIndex:0];
contentView.height -= 49;
self.tabBarController.tabBar.hidden = NO;

仅仅这样写的话,会出现问题的,(如果不做处理的话,从第三个页面进入到第四个页面,将不会隐藏tabBar)

比较完善的做法是这样的

虽然会在使用右滑手势返回pop时,界面有些不太雅观,但还是可以接受的😄

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

     // 强制显示tabbar
     NSArray *views = self.tabBarController.view.subviews;
     UIView *contentView = [views objectAtIndex:0];
     contentView.height -= 49;
     self.tabBarController.tabBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // 强制隐藏tabbar
    NSArray *views = self.tabBarController.view.subviews;
    UIView *contentView = [views objectAtIndex:0];
    contentView.height += 49;
    self.tabBarController.tabBar.hidden = YES;
}