iphone - 横向隐藏 UITabBar

标签 iphone ios uitabbarcontroller uitabbar

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

1) 使用导航 Controller ,可以使用 hidesBottomBarWhenPushed 属性在推送之前隐藏下一个 vc 的选项卡栏。 Typical answer here .

2) 浏览选项卡栏 Controller 的 View 层次结构并修改选项卡栏的框架和/或可见性。 Typical answer here .

但在我看来,这两种答案都不够。 1)如果我们需要隐藏当前 View 上的选项卡栏,比如旋转到横向时,该怎么办? 2) 通过 Apple 库的私有(private) View 层次结构唤醒的半页代码是:繁琐,b.容易发生不可预见的破坏,c.可能是应用程序审批的拦截器。

那么应用程序可以做什么呢?答案是不允许吗?有苹果文档引用支持吗?这将是一个悲伤的答案。在我看来,旋转情况是隐藏选项卡栏的合理原因。

预先感谢您的帮助。

最佳答案

抱歉回复延迟,但我已经提取了代码,您可以看到我如何旋转设备以仅横向全屏显示“ map View ”。

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

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

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

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

关于iphone - 横向隐藏 UITabBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12181935/

相关文章:

ios - 在 IOS 中动态添加新标签

ios - UISegmentedControl 隐藏在 titleBar 下

iphone - UIToolBar 与 UITableView - TableView 加载时不刷新

c# - 使用自动布局约束时,Syncfusion SfRangeSlider slider 和标签未正确对齐

ios - 如何在swift 3中做障碍

ios - 从解析查询 block 返回 UIImage 数组

php - 图像自动旋转

ios - 是否可以在 iOS 模拟器中禁用网络?

iphone - UIImageView 放大和缩小

iphone - CoreLocation GPS 状态栏指示器不消失