UITabBar 底部导航栏的属性设置(颜色字号)

本文介绍了在iOS 13.0之前如何设置UITabBar的颜色和字号,并针对iOS 13.0后出现的bug进行详细分析,包括一般设置的尝试以及最终解决未选中项颜色问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.iOS 13.0之前的设置代码(在UITabBarController里面)

    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:k_title_color_2B,NSFontAttributeName:TEXT_LIT_B_FONT10} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:k_main_tab_text_color,NSFontAttributeName:TEXT_LIT_B_FONT10} forState:UIControlStateSelected];

    
    self.delegate = self;
    self.tabBar.alpha = 1.0f;
    //解决这个问题是 iOS 12.1 Beta 2 引入的问题,只要 UITabBar 是磨砂的,并且 push viewController 时 hidesBottomBarWhenPushed = YES 则手势返回的时候就会触发,出现这个现象的直接原因是 tabBar 内的按钮 UITabBarButton 被设置了错误的 frame,frame.size 变为 (0, 0) 导致的。
    [UITabBar appearance].translucent = NO;
    

2.iOS 13.0 之后出现bug 

(1)一般设置(出现问题选中可以设置 未选中的一直爽默认状态)

       

        tabbar.tintColor = k_main_tab_text_color;
        tabbar.unselectedItemTintColor = k_title_color_2B;

上面本人设置了unselectedItemTintColor 但是还是默认颜色

(2)最终解决方案

    if (@available(iOS 13.0, *)) {
        //这个是操作改变选中未选中的
        UITabBarItemAppearance *item_Appearance = [[UITabBarItemAppearance  alloc] init];
        [item_Appearance.normal setTitleTextAttributes:@{ NSForegroundColorAttributeName:k_title_color_2B,NSFontAttributeName:TEXT_LIT_B_FONT10}];
        [item_Appearance.selected setTitleTextAttributes:@{ NSForegroundColorAttributeName:k_main_tab_text_color,NSFontAttributeName:TEXT_LIT_B_FONT10}];

        //导航栏其它属性设置
        UITabBarAppearance *appearance = [self.tabBar.standardAppearance copy];
           appearance.backgroundColor = k_main_tab_bar_color;
        appearance.backgroundImage = [UIImage imageNamed:@"tab_bkg_bai"];
        //将item赋值过来
        appearance.stackedLayoutAppearance = item_Appearance; //
        tabbar.standardAppearance =appearance;
        //此代码可以改变选中颜色
        tabbar.tintColor = k_main_tab_text_color;
        if (@available(iOS 15.0, *)) {
            tabbar.scrollEdgeAppearance =appearance;
        }
    }else{
//        tabbar.barTintColor = k_main_tab_bar_color;
        tabbar.backgroundImage = [UIImage imageNamed:@"tab_bkg_2"];
        [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tab_bkg_2"]];
    }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值