如何在 iOS 中使用导航栏隐藏/显示视图的标签栏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6383069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:23:56  来源:igfitidea点击:

How to hide/show tab bar of a view with a navigation bar in iOS?

iosuinavigationcontrolleruitabbarcontroller

提问by dork

I have views with a navigation bar and a tab bar. What I would like to happen is to hide the tab bar on a certain view and show the tab bar again when the user changes views.

我有一个导航栏和一个标签栏的视图。我想要发生的是隐藏某个视图上的标签栏,并在用户更改视图时再次显示标签栏。

I saw a snippet of code for hiding the tab bar:

我看到了一段隐藏标签栏的代码:

-(void)makeTabBarHidden:(BOOL)hide
{
    // Custom code to hide TabBar
    if ( [tabBarController.view.subviews count] < 2 ) {
        return;
    }

    UIView *contentView;

    if ( [[tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] ) {
        contentView = [tabBarController.view.subviews objectAtIndex:1];
    } else {
        contentView = [tabBarController.view.subviews objectAtIndex:0];
    }

    if (hide) {
        contentView.frame = tabBarController.view.bounds;       
    }
    else {
        contentView.frame = CGRectMake(tabBarController.view.bounds.origin.x,
             tabBarController.view.bounds.origin.y,
             tabBarController.view.bounds.size.width,
             tabBarController.view.bounds.size.height - tabBarController.tabBar.frame.size.height);
    }

    tabBarController.tabBar.hidden = hide;
}

from: http://nickwaynik.com/iphone/hide-tabbar-in-an-ios-app/

来自:http: //nickwaynik.com/iphone/hide-tabbar-in-an-ios-app/

I call this on the view wherein I want the tab bar hidden

我在想要隐藏标签栏的视图上调用它

[self makeTabBarHidden:YES];

it works fine when i show/hide it on that view but when I navigate back to the previous view, the tab bar there is also hidden. I tried calling that function in the view's viewDidUnload, viewWillDisappear, viewDidDisappearfunctions but nothing happens. The same is true when the function is called in the previous view's viewDidLoad, viewWillAppear, viewDidAppearfunctions.

当我在该视图上显示/隐藏它时它工作正常,但是当我导航回上一个视图时,标签栏也被隐藏了。我尝试在视图的viewDidUnload, viewWillDisappear,viewDidDisappear函数中调用该函数,但没有任何反应。在前面视图的viewDidLoad, viewWillAppear,viewDidAppear函数中调用该函数时也是如此。

回答by ushika

You can set the UIViewController.hidesBottomBarWhenPushed instead:

你可以设置 UIViewController.hidesBottomBarWhenPushed 代替:

DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.hidesBottomBarWhenPushed = YES;
[[self navigationController] pushViewController:detailViewController animated:YES];    
[detailViewController release];

回答by Suragch

You can also do this in the Interface Builder for a storyboard. Select the View Controller that you want to hide the Tab Bar for and then select "Hide Bottom Bar on Push".

您也可以在界面生成器中为情节提要执行此操作。选择要为其隐藏 Tab Bar 的视图控制器,然后选择“Hide Bottom Bar on Push”。

enter image description here

在此处输入图片说明

回答by boliva

I just created a category on UITabBarController that allows you to hide the TabBar, optionally with an animation:

我刚刚在 UITabBarController 上创建了一个类别,允许您隐藏 TabBar,可选择使用动画:

https://github.com/idevsoftware/Cocoa-Touch-Additions/tree/master/UITabBarController_setHidden

https://github.com/idevsoftware/Cocoa-Touch-Additions/tree/master/UITabBarController_setHidden

It adds the tabBarHiddenproperty (with isTabBarHiddenas its getter) and the - (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animatedmethod.

它添加了tabBarHidden属性(isTabBarHidden作为它的 getter)和- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated方法。

回答by Yogesh Dalavi

self.navigationController.hidesBottomBarWhenPushed=YES;

Add this line to your viewDidLoador viewWillAppear; this will hide you tab from bottom.

将此行添加到您的viewDidLoadviewWillAppear;这将从底部隐藏你的标签。

回答by alicanozkara

Try this for hide / show:

试试这个隐藏/显示:

- (void)viewWillDisappear:(BOOL)animated {
    self.hidesBottomBarWhenPushed = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    self.hidesBottomBarWhenPushed = YES;
}

回答by Sandu

Swift 3: Set tab bar to hide in viewWillAppearor viewDidAppear

Swift 3:将标签栏设置为隐藏viewWillAppearviewDidAppear

self.tabBarController?.tabBar.isHidden = true

回答by ShowPony

The same property is available on the attributes inspector when you click on your view controller on your Xib or storyboard file.

当您单击 Xib 或故事板文件上的视图控制器时,属性检查器上可以使用相同的属性。

回答by Soropromo

you can use below code but tabBarremains hidden when you navigate back.

您可以使用以下代码,但tabBar在您返回时保持隐藏状态。

    //hide tabbar
    //self.tabBarController?.tabBar.isHidden = true

better wayis to do through main.storyboard check "Hide Bottom Bar on Push"as I've done.

更好的方法"Hide Bottom Bar on Push"像我一样通过 main.storyboard 检查。

enter image description here

在此处输入图片说明