隐藏后显示标签栏

发布于 2024-09-15 17:00:54 字数 130 浏览 7 评论 0原文

有没有办法在隐藏标签栏后显示它?

有一个标签栏导航结构。对于其中一个选项卡,我需要隐藏其第二级和第三级视图的选项卡栏。但同时我需要展示它的第一个和第四个视图。

我认为 Elements 中的示例代码在这里并不适用。

Is there any way to show a tab bar after it has been hidden?

Got a tabbar-nav structure. For one of the tabs, I need to hide the tab bar for its 2nd and 3rd level view. But at the same time I will need to show its 1st and 4th view.

The sample code from Elements isn't really applicable here I think.

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

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

发布评论

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

评论(4

相权↑美人 2024-09-22 17:00:54

我找到了一个很好的实用解决方案来解决这个问题 - 使 UITabBarController 的视图大于它需要的大小,以便实际的 UITabBar 被屏幕剪切。

假设标签栏视图通常填充其超级视图,这种事情应该起作用:

CGRect frame = self.tabBarController.view.superview.frame;
if (isHidden)
{
    CGFloat offset = self.tabBarController.tabBar.frame.size.height;
    frame.size.height += offset;
}
self.tabBarController.view.frame = frame;

标签栏仍然显示,但它不在屏幕底部,所以看起来已经被隐藏了。

如果它导致额外的削波,它可能会对性能产生影响,但到目前为止,它似乎有效。

I've found quite a good pragmatic solution to this problem - make the UITabBarController's view larger than it needs to be, so that the actual UITabBar is clipped by the screen.

Assuming that the tab bar view normally fills its superview, this sort of thing should work:

CGRect frame = self.tabBarController.view.superview.frame;
if (isHidden)
{
    CGFloat offset = self.tabBarController.tabBar.frame.size.height;
    frame.size.height += offset;
}
self.tabBarController.view.frame = frame;

The tab bar is still showing, but it's off the bottom of the screen, so appears to have been hidden.

It might have performance implications if it causes extra clipping, but so far, it seems to work.

Hello爱情风 2024-09-22 17:00:54

推送到导航堆栈上的 UIViewController 可以执行如下操作:

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO; // Or YES as desired.
}

编辑:在下面添加了额外的代码来处理框架。我不认为我特别推荐这个想法,因为它依赖于 UITabBarController 的内部默认视图结构。

在 UITabBarController 上定义以下类别:

@interface UITabBarController (Extras)
- (void)showTabBar:(BOOL)show;
@end

@implementation UITabBarController (Extras)
- (void)showTabBar:(BOOL)show {
    UITabBar* tabBar = self.tabBar;
    if (show != tabBar.hidden)
        return;
    // This relies on the fact that the content view is the first subview
    // in a UITabBarController's normal view, and so is fragile in the face
    // of updates to UIKit.
    UIView* subview = [self.view.subviews objectAtIndex:0];
    CGRect frame = subview.frame;
    if (show) {
        frame.size.height -= tabBar.frame.size.height;
    } else {
        frame.size.height += tabBar.frame.size.height;
    }
    subview.frame = frame;
    tabBar.hidden = !show;
}
@end

然后,不要使用我最初建议的 tabBar.hidden 更改,而是执行以下操作:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBarController showTabBar:NO];
}

显然,要确保实现已包含类别定义,以便知道“showTabBar”。

The UIViewControllers that are pushed onto the navigation stack can do the something like the following:

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO; // Or YES as desired.
}

EDIT: Added additional code below to deal with the frame. Don't think I particular recommend this idea since it relies on the internal default view structure of a UITabBarController.

Define the following category on UITabBarController:

@interface UITabBarController (Extras)
- (void)showTabBar:(BOOL)show;
@end

@implementation UITabBarController (Extras)
- (void)showTabBar:(BOOL)show {
    UITabBar* tabBar = self.tabBar;
    if (show != tabBar.hidden)
        return;
    // This relies on the fact that the content view is the first subview
    // in a UITabBarController's normal view, and so is fragile in the face
    // of updates to UIKit.
    UIView* subview = [self.view.subviews objectAtIndex:0];
    CGRect frame = subview.frame;
    if (show) {
        frame.size.height -= tabBar.frame.size.height;
    } else {
        frame.size.height += tabBar.frame.size.height;
    }
    subview.frame = frame;
    tabBar.hidden = !show;
}
@end

Then, instead of using the tabBar.hidden change I originally suggested, do the following:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBarController showTabBar:NO];
}

Obviously making sure that the implementation has included the category definition so that 'showTabBar' is known.

你げ笑在眉眼 2024-09-22 17:00:54

您需要实现一个委托方法

- (BOOL)tabBarController:(UITabBarController *)tabBarController2 shouldSelectViewController:(UIViewController *)viewController

,在其中您可以检查选择了哪个索引并显示选项卡栏

if([[tabBarController.viewControllers objectAtIndex:0] isEqual:viewController])// it is first tab
{
      tabBarController.tabBar.hidden = FALSE;
}

You need to implement a delegate method

- (BOOL)tabBarController:(UITabBarController *)tabBarController2 shouldSelectViewController:(UIViewController *)viewController

Inside that you can check which index is selected and show the tab bar

if([[tabBarController.viewControllers objectAtIndex:0] isEqual:viewController])// it is first tab
{
      tabBarController.tabBar.hidden = FALSE;
}
晨曦慕雪 2024-09-22 17:00:54

我知道这是一篇旧帖子,但我认为下面的代码将有助于隐藏您不希望打开的视图控制器上的选项卡栏,并且具有当您从该视图控制器返回时自动读取选项卡栏的额外好处

UIViewController *hideTabbarViewController = [[UIViewController alloc] init];  
hideTabbarViewController.hidesBottomBarWhenPushed = YES;  
[[self navigationController] hideTabbarViewController animated:YES]; 

I know this is an old post but i think the below code would help to hide the tabbar on the viewcontroller you don't want it on and has the added benefit of automatically readding the tabbar when you come back from that view controller

UIViewController *hideTabbarViewController = [[UIViewController alloc] init];  
hideTabbarViewController.hidesBottomBarWhenPushed = YES;  
[[self navigationController] hideTabbarViewController animated:YES]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文