0

如何以编程方式隐藏 UITabBar?在 SO 上已多次询问和回答,但答案似乎大致有两种:

1)使用导航控制器,可以使用 hidesBottomBarWhenPushed 属性在推送之前隐藏下一个 vc 的标签栏。 这里的典型答案

2)遍历标签栏控制器的视图层次结构并修改标签栏的框架和/或可见性。 这里的典型答案

但是这两种答案都不足,imo。1)如果我们需要在我们所在的视图上隐藏标签栏怎么办,比如当旋转到横向时。2)通过Apple库的私有视图层次结构唤醒的半页代码是a。笨重的湾 容易发生不可预见的断裂,c。可能是应用程序批准的阻止程序。

那么要做什么应用呢?答案是不允许吗?是否有苹果文档参考支持这一点?这将是一个悲伤的答案。Imo,旋转案例是隐藏标签栏的正当理由。

在此先感谢您的帮助。

4

3 回答 3

1

抱歉响应延迟,但我已经提取了我的代码,您可以看到我如何旋转我的设备以仅在横向中全屏显示“地图视图”。

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    [self hideTabBar:self.tabBarController];
    [self.view bringSubviewToFront:self.eventsMapView];
    self.eventsMapView.bounds = self.view.bounds;
    self.eventsMapView.frame = CGRectMake(0, -208, self.view.frame.size.width, 300);
} else if(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || toInterfaceOrientation == UIInterfaceOrientationPortrait) {
    [self showTabBar:self.tabBarController];
    [self.view sendSubviewToBack:self.eventsMapView];
}

}

由于我们调用其中的方法来实际隐藏和显示选项卡栏,因此我们还需要在 .m 文件中定义这些方法:

#pragma mark - Tab Bar Methods -

-(void)hideTabBar:(UITabBarController *)tabbarcontroller {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    for(UIView *view in tabbarcontroller.view.subviews) {
        if([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
        } else {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
        }
    }

    [UIView commitAnimations];   
}

-(void)showTabBar:(UITabBarController *)tabbarcontroller {       
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    for(UIView *view in tabbarcontroller.view.subviews) {
        if([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
        } else {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
        }
    }

    [UIView commitAnimations]; 
}

如果您已经在选项卡栏控制器中,那么您需要确保每个子项(或单个选项卡 ViewController)都返回 TRUE,如下所示。

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return TRUE;

}

希望这会有所帮助 - 如果您有任何问题,请发表评论,我会更新我的答案以更好地展示它。

于 2012-08-30T14:38:50.087 回答
0

你可以在这里找到一些有用的代码。您可以从您的 shouldrotate: 方法中调用 hideTabbar

于 2012-08-29T18:42:58.133 回答
0

2021年的答案。Xcode 12. 在横向模式下创建新布局。在下图中,我创建了 wAny hCompact (hC) 属性,然后将其设置为隐藏。

在此处输入图像描述

于 2021-01-02T13:01:47.773 回答