天天看點

iOS下完美隐藏tabBar和navigationBar 的方案 iOS下完美隐藏tabBar和navigationBar 的方案。

注:本文所有圖檔儲存EverNote 印象筆記(www.yinxiang.com)當中, 如果有其帳号登入後即可檢視全部圖檔。 如沒有帳号可用e-mail 注冊免費印象筆記

iOS下完美隐藏tabBar和navigationBar 的方案。

tags: 

IOS

教程

應用描述

我做一個小應用需要有如下功能,當點選一個tabBar的子視窗時,需要隐藏導覽列和tab工具欄。再次點選時,又重新顯示。

以下正常情況(為示範清楚,我畫上兩條紅帶):

iOS下完美隐藏tabBar和navigationBar 的方案 iOS下完美隐藏tabBar和navigationBar 的方案。

不成功的辦法

第一種辦法:參考一個項目源碼,将tabBar的alpha設為0,這樣的确可以隐藏工具欄,但是會在工具欄的區域顯示一個黑條。代碼如下

+ (void)hideTabBar:(UIView*)tabBar hide:(BOOL)hide animated:(BOOL)animated
{
    //UIView*tabBar = self.tabBarController.tabBar;

    if (animated)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDelegate:nil];
        [UIView setAnimationDuration:0.5];
    }
    [tabBar setAlpha:hide ? 0.0 : 1.0];

    if (animated)
    {
        [UIView commitAnimations];
    }
}
           

顯示效果如下:

iOS下完美隐藏tabBar和navigationBar 的方案 iOS下完美隐藏tabBar和navigationBar 的方案。

另一種方法是 設定這個對象的hidden 屬性,設為YES就是隐藏 如

self.tabBarController.tabBar.hidden = YES;
           

但是效果同上,仍然不成功。

成功方法

後來找到參考 http://code4app.com/snippets/one/IOS7-%E9%9A%90%E8%97%8FTabbar-%E5%87%BA%E7%8E%B0%E7%9A%84%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95/52b502cccb7e84ce558b48c9

測試成功,點選後效果

iOS下完美隐藏tabBar和navigationBar 的方案 iOS下完美隐藏tabBar和navigationBar 的方案。

這個代碼的思路,當隐藏時,是把中間顯示的content View,擴充到全屏,當顯示時間又收縮成原樣。

我現在把這兩個方法寫成一個靜态工具方法,大家隻要把目前UIViewController的指針傳入即可。

+ (void)hideTabBar:(UIViewController*)viewController{

    if( (viewController.tabBarController == NULL) || (viewController.tabBarController.tabBar == NULL))
        return ;

    UINavigationBar * navbar = NULL;
    UITabBar *tabBar = viewController.tabBarController.tabBar;


    if( (viewController.navigationController.navigationBar != NULL) ||
            (viewController.navigationController.navigationBar != NULL) )
        navbar = viewController.navigationController.navigationBar;

    //内容顯示區
    UIView *contentView;

    if ( [[viewController.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
        contentView = [viewController.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [viewController.tabBarController.view.subviews objectAtIndex:0];


    if(tabBar.hidden == NO)
    {


        [ contentView setFrame:CGRectMake(contentView.bounds.origin.x,contentView.bounds.origin.y,
                                          contentView.bounds.size.width, contentView.bounds.size.height +
                                          tabBar.frame.size.height)];

        [ tabBar setHidden:YES ];
        [ navbar setHidden:YES ];

    }
    else {


        [contentView setFrame:CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height - tabBar.frame.size.height)];

        [ tabBar setHidden:NO ];
        [ navbar setHidden:NO ];
    }




}           

如圖檔不正常請通路 完整版 

https://app.yinxiang.com/shard/s1/sh/4d2992b8-97e1-44c2-bd5d-75ba0115ef50/65253dc9a9339b91c2712c3bb63bbafd