簡體   English   中英

在橫向中隱藏 UITabBar

[英]Hide UITabBar in landscape

如何以編程方式隱藏 UITabBar? 已經在 SO 上多次詢問和回答,但答案似乎大致分為兩種:

1) 使用導航控制器,可以在推送之前使用 hidesBottomBarWhenPushed 屬性隱藏下一個 vc 的標簽欄。 典型的答案在這里

2) 遍歷標簽欄控制器的視圖層次結構並修改標簽欄的框架和/或可見性。 典型的答案在這里

但是兩種答案都不夠,imo。 1) 如果我們需要在我們所在的視圖上隱藏標簽欄怎么辦,比如旋轉到橫向時。 2) 通過 Apple 庫的私有視圖層次結構喚醒的半頁代碼是 a. 麻煩的 b. 容易發生意外斷裂,c。 可能是應用程序批准的阻止程序。

那么應用程序可以做什么呢? 答案是不允許嗎? 是否有蘋果文檔參考支持? 這將是一個悲傷的答案。 Imo,旋轉案例是隱藏標簽欄的合法原因。

在此先感謝您的幫助。

很抱歉延遲響應,但我已經提取了我的代碼,您可以看到我如何旋轉我的設備以僅在橫向全屏顯示“地圖視圖”。

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

如果您已經在 Tab Bar Controller 中,那么您需要確保每個子項(或單個選項卡 ViewController)為如下所示的方向返回 TRUE。

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

}

希望這會有所幫助 - 如果您有任何問題,請發表評論,我會更新我的答案以更好地展示它。

您可以在此處找到一些有用的代碼。 您可以從 shouldrotate: 方法中調用 hideTabbar

2021 答案。 Xcode 12. 在橫向模式下創建一個新布局。 在下圖中,我創建了 wAny hCompact (hC) 屬性,然后將其設置為隱藏。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM