iOS導(dǎo)航欄和底部tabbar的隱藏和背景色

我項(xiàng)目的主頁常見的是UITabbarController + UINavigationController形式

屏幕快照 2016-07-17 下午7.54.45.png

因?yàn)閷?dǎo)航欄是自定義的,所以需要把所有導(dǎo)航欄都隱藏掉,再在每個(gè)界面自己寫導(dǎo)航欄視圖(感覺好坑啊,以后應(yīng)該會(huì)改)

因此,我寫了重寫了
一個(gè)繼承于UINavigationController的NavigationController
和一個(gè)繼承于UITabBarController的TabBarController。

一. TabBarController中的代碼如下:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the
    
    //A,(B、C一樣)
    ViewControllerA *aVC = [ViewControllerA new];
    aVC.tabBarItem.title = @"主頁";
    aVC.tabBarItem.image = [[UIImage imageNamed:@"a_unselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    aVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"a_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    NavigationController *aNavi = [[NavigationController alloc] initWithRootViewController:aVC];
    
    //設(shè)置tabBar背景色
    [self.tabBar setBackgroundImage:[Tool createImageWithColor:UIColorFromHex(0x63DDA1)]];
    //字體顏色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: UIColorFromHex(0xEE9754)} forState:UIControlStateSelected];
    
    self.viewControllers = @[aNavi, bNavi, cNavi];
}
二. 在NavigationController中

之前的寫法(×):

//重寫方法[pushViewController: animated:]
//可攔截NavigationController子控制器中所有的push操作,
//因此在其中設(shè)置一句隱藏代碼,其他界面就都不用設(shè)置了。
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //隱藏tabbar,(A、B、C界面上不隱藏,其他界面都隱藏)
    //一定要在push之前隱藏,否則無效,
    //所以,如果在調(diào)用 [super pushViewController: animated:] 后再寫判斷隱藏是無效的
    if ([viewController isKindOfClass:[ViewControllerA class]] ||
        [viewController isKindOfClass:[ViewControllerB class]] ||
        [viewController isKindOfClass:[ViewControllerC class]])
    {
        viewController.hidesBottomBarWhenPushed = NO;
    }
    else {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    //先調(diào)用父類push
    [super pushViewController:viewController animated:animated];
    //隱藏導(dǎo)航欄,
    [viewController.navigationController setNavigationBarHidden:YES];
}

更正(√):

//舊的寫法不僅繁瑣,而且,當(dāng)要push的頁面在tabBar中也有時(shí)處理會(huì)很麻煩。
//只要navigationController.viewControllers的第一個(gè)控制器隱藏就行了
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ((self.viewControllers.count > 0) && (viewController != self.viewControllers[0])) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
    [viewController.navigationController setNavigationBarHidden:YES];
}

然后就ok了。

另外,關(guān)于TabBar設(shè)置背景顏色,我遇到兩個(gè)坑

使用代碼直接設(shè)置背景顏色居然無效!

//無效
[[UITabBar appearance] setBackgroundColor:UIColorFromHex(0x63DDA1)];

百度了下得設(shè)置背景圖片才有效,經(jīng)試驗(yàn) 下面3中設(shè)置方法均有效

//1.
[self.tabBar setBackgroundImage:[Tool createImageWithColor:UIColorFromHex(0x63DDA1)]];
//2.
[[UITabBar appearance] setBackgroundImage:[Tool createImageWithColor:UIColorFromHex(0x63DDA1)]];

//3. 第3種千萬別忘了設(shè)置self.tabBar.opaque = YES,否則還是會(huì)出現(xiàn)tabBar的效果
UIView *tabBarBgColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tabBar.bounds.size.width, self.tabBar.bounds.size.height)];
tabBarBgColorView.backgroundColor = UIColorFromHex(0x63DDA1);
[self.tabBar insertSubview:tabBarBgColorView atIndex:0];
self.tabBar.opaque = YES;
把16進(jìn)制轉(zhuǎn)換成顏色的宏定義:
#define UIColorFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0 green:(((s & 0xFF00) >> 8))/255.0 blue:((s & 0xFF))/255.0 alpha:1.0]
#define UIColorFromHexA(s, a) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0 green:(((s & 0xFF00) >> 8))/255.0 blue:((s & 0xFF))/255.0 alpha:a]
#define UIColorFromRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
用顏色生成圖片的代碼:
//用顏色創(chuàng)建一張圖片
+ (UIImage *)createImageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,547評(píng)論 1 14
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,245評(píng)論 4 61
  • 總是在孤獨(dú)無助的時(shí)候黯然神傷,內(nèi)心被恐懼和脆弱所吞噬。 大學(xué)畢業(yè)已經(jīng)三年了,時(shí)光一晃而過,我還是那個(gè)我,沒有變強(qiáng)大...
    冷笒冰閱讀 329評(píng)論 0 2
  • 這兩天,經(jīng)過一些事情的串聯(lián),發(fā)現(xiàn)自己在選擇面前,做出改變或者不改變的選擇還是受到自己不想改變現(xiàn)狀的限制。 做出的選...
    Ceeport_寶閱讀 741評(píng)論 0 0
  • 自由寫作.轉(zhuǎn)型與蛻變第二期第1篇 因?yàn)橐獪?zhǔn)備新一年的進(jìn)店了,這幾天在研究CEM的資料。 CEM的宗旨是“打造客戶鐘...
    真言臻宇閱讀 546評(píng)論 0 5