簡體   English   中英

如何隱藏標簽欄控制器?

[英]How to Hide Tab Bar Controller?

如何隱藏標簽欄控制器? 我想通過雙擊 UIImageView 來隱藏 Tab Bar 控制器。

試試這個代碼:

[self.tabBarController.tabBar setHidden:YES];

需要定義 tabbarcontroller 的地方...

編輯

AppDelegateFileName *appDelegate = (AppDelegateFileName *) [[UIApplication sharedApplication] delegate];
[appDelegate.tabbarController.tabBar setHidden:YES];

在執行此操作之前,請確保在tabbarController .h 文件中創建了tabbarController聲明。

如果使用 Storyboards,您可以簡單地取消選中 ViewController 的屬性檢查器中的復選框。 它被稱為“在推送時隱藏底部欄”。 確實非常方便,並且在從無 tabBar 的 viewController 導航回來后無需再次處理 tabBar 的顯示。 我不知道這是在哪個 XCode 版本中引入的,但它適用於 XCode 6 + 。

使用 Tap Gesture Recognizer 檢測UIImageView上的雙擊。 然后調用檢測雙擊的方法。 在該方法中添加以下代碼行。

self.tabBarController.tabBar.hidden=YES;

希望這可以幫助。

使用下面的代碼以動畫樣式隱藏/顯示標簽欄控制器。
hiddenTabBar是一個BOOL變量。

- (void) hidetabbar {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.0];

    for(UIView *view in objtabbar.view.subviews)
    {

        if([view isKindOfClass:[UITabBar class]])
        {

            if (hiddenTabBar) {
                [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
            }
        } else {
            if (hiddenTabBar) {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
            }

        }
    }

    [UIView commitAnimations];

    hiddenTabBar = !hiddenTabBar;
}

UIViewController 有一個屬性

@property(nonatomic, readonly, retain) UITabBarController *tabBarController

您可以設置:

self.tabBarController.tabBar.hidden = YES;

斯威夫特 2.1:

self.tabBarController!.tabBar.hidden = true

目標-C

[self.tabBarController.tabBar setHidden:YES];

斯威夫特 3

self.tabBarController?.tabBar.isHidden = true

斯威夫特 2

self.tabBarController?.tabBar.hidden = true

目標-C

[self.tabBarController.tabBar setHidden:YES];

目標 C 2.0

self.tabBarController.tabBar.hidden = YES;

iOS 9 之前的 Swift

tabBarController?.tabBar.hidden = true

Swift iOS 9 及以上

tabBarController?.tabBar.isHidden = true

Swift 5 及更高版本的額外技巧:如果您想更改隱藏屬性,請切換它

if let t = tabBarController?.tabBar {
    t.isHidden = t.!isHidden
}
// is equal to 
tabBarController?.tabBar.isHidden.toggle()

多個根視圖控制器的 Swift 解決方案

如果您或某人需要在自定義控制器中隱藏標簽欄,對於使用多個rootViewController的應用程序, rootViewController嘗試以下操作:

//instantiate appDelegate in your controller first
let appDelegate = UIApplication.shared.delegate as! AppDelegate

//then just hide the tab bar as following
appDelegate.window?.rootViewController?.tabBarController?.tabBar.isHidden = true

將視圖推送到新視圖時試試這個:

self.tabbarconroller.tabbar.hidden = YES;

暫無
暫無

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

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