如何在 iOS 中隐藏/显示带有导航栏的视图的选项卡栏?

发布于 2024-11-16 00:59:17 字数 1502 浏览 2 评论 0原文

我有带有导航栏和选项卡栏的视图。我想要发生的事情是隐藏某个视图上的选项卡栏,并在用户更改视图时再次显示选项卡栏。

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

-(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;
}

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

我在视图上调用它,其中我希望隐藏选项卡栏,

[self makeTabBarHidden:YES];

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

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/

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, viewDidDisappear functions but nothing happens. The same is true when the function is called in the previous view's viewDidLoad, viewWillAppear, viewDidAppear functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

上课铃就是安魂曲 2024-11-23 00:59:17

您可以设置 UIViewController.hidesBottomBarWhenPushed 来代替:

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

You can set the UIViewController.hidesBottomBarWhenPushed instead:

DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.hidesBottomBarWhenPushed = YES;
[[self navigationController] pushViewController:detailViewController animated:YES];    
[detailViewController release];
无力看清 2024-11-23 00:59:17

您还可以在故事板的 Interface Builder 中执行此操作。选择要隐藏选项卡栏的视图控制器,然后选择“按下时隐藏底部栏”。

输入图像描述这里

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".

enter image description here

つ可否回来 2024-11-23 00:59:17

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

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

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

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

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

It adds the tabBarHidden property (with isTabBarHidden as its getter) and the - (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated method.

带上头具痛哭 2024-11-23 00:59:17

斯威夫特3:
viewWillAppearviewDidAppear 中设置标签栏隐藏

self.tabBarController?.tabBar.isHidden = true

Swift 3:
Set tab bar to hide in viewWillAppear or viewDidAppear

self.tabBarController?.tabBar.isHidden = true
爱的故事 2024-11-23 00:59:17

尝试隐藏/显示:

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

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

Try this for hide / show:

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

- (void)viewWillAppear:(BOOL)animated {
    self.hidesBottomBarWhenPushed = YES;
}
也只是曾经 2024-11-23 00:59:17
self.navigationController.hidesBottomBarWhenPushed=YES;

将此行添加到您的 viewDidLoadviewWillAppear 中;这将从底部隐藏您的选项卡。

self.navigationController.hidesBottomBarWhenPushed=YES;

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

夕色琉璃 2024-11-23 00:59:17

当您单击 Xib 或故事板文件上的视图控制器时,属性检查器上也提供相同的属性。

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

淡莣 2024-11-23 00:59:17

您可以使用下面的代码,但当您向后导航时,tabBar 仍然隐藏。

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

更好的方法是通过 main.storyboard 来完成
像我一样检查“按下时隐藏底部栏”。

输入图片此处描述

you can use below code but tabBar remains hidden when you navigate back.

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

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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文