一、首先看一下 tabBar 的UI 显示的层级结构

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

解决方法如下代码展示

+(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: 半透明的
}
//设置全局界面的颜色
    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
    }

原文地址:https://blog.csdn.net/same_life/article/details/126198910

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_15285.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注