iOS13 tabBar 设置背景色失效

一、首先看一下 tabBar 的UI 显示的层级结构
  • 如果 UITabBarController 有 N 个子控制器,那么 UITabBar 的内部就会有 N 个 UITabBarButton 作为子控件。
    在这里插入图片描述
  • UITabBarButton 里面显示什么内容,由对应子控制器的 tabBarItem 属性决定。
    在这里插入图片描述

二、升级到 iOS 13后,之前正常显示的 tabBar 的背景色设置失效了

解决方法如下,代码展示:

  • oc 版本
+(void)initialize {
    NSDictionary *attrNormal = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:kLightGrayColor};
    NSDictionary *attrSelect = [NSDictionary dictionary];
    UITabBar *tabBar = [UITabBar appearance];
    //ios 13 之后需要这样设置才有效
    if (@available(iOS 13.0, *)) {
        attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor labelColor]};
        UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc]init];
        //设置tabar背景色
        tabBarAppearance.backgroundColor = [UIColor secondarySystemGroupedBackgroundColor];
        tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = attrNormal;
        tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = attrSelect;
        //必须要加上这两句
        tabBar.standardAppearance = tabBarAppearance;
        if (@available(iOS 15.0, *)) {
            tabBar.scrollEdgeAppearance = tabBarAppearance;
        } else {
            // Fallback on earlier versions
        }
    } else {
        attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:kBlackColor};
        UITabBarItem *tbItem = [UITabBarItem appearance];
        [tbItem setTitleTextAttributes:attrNormal forState:UIControlStateNormal];
        [tbItem setTitleTextAttributes:attrSelect forState:UIControlStateSelected];
        [tabBar setBarTintColor:kWhiteColor];  //tabBar的背景色
    }
    tabBar.translucent = YES;     //translucent: 半透明的
}
  • Swift 版本
//设置全局界面的颜色
    func setupAppearance() {
        //设置加粗字体: Helvetica-Bold
        let attrNomal = [NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 18), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
        let attrSelect = [NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 18), NSAttributedString.Key.foregroundColor: UIColor.black]
        let customTabBar = UITabBar.appearance()
        if #available(iOS 13.0, *) {
            let tabBarAppearance = UITabBarAppearance()
            //设置tabar背景色
            tabBarAppearance.backgroundColor = .secondarySystemGroupedBackground
            tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = attrNomal as [NSAttributedString.Key : Any]
            tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = attrSelect as [NSAttributedString.Key: Any]
            customTabBar.standardAppearance = tabBarAppearance
            if #available(iOS 15.0, *) {
                customTabBar.scrollEdgeAppearance = tabBarAppearance
            } else {
                
            }
        } else {
            let tabBarItem = UITabBarItem.appearance()
            tabBarItem.setTitleTextAttributes(attrNomal as [NSAttributedString.Key: Any], for: .normal)
            tabBarItem.setTitleTextAttributes(attrSelect as [NSAttributedString.Key : Any], for: .selected)
            //tabBar的背景色
            customTabBar.barTintColor = .white
        }
        //translucent: 半透明的
        customTabBar.isTranslucent = true
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值