iOS13.0以后修改tabbar文字颜色和顶部横线消失不起作用

主要是因为13.0系统以后采用了新方法,原来的不起作用了,直接上代码:

 if (@available(iOS 13.0, *)) {
    UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc] init];
    NSMutableDictionary<NSAttributedStringKey, id> *selectedAttributes = self.tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes.mutableCopy;
    selectedAttributes[NSForegroundColorAttributeName] = kBlackColor;
    tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes.copy;

    NSMutableDictionary<NSAttributedStringKey, id> *normalAttributes = self.tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes.mutableCopy;
    normalAttributes[NSForegroundColorAttributeName] = kBlackColor;
    tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes.copy;

    tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName : kBlackColor};
    tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName : kBlackColor};
    
    tabBarAppearance.backgroundImage = [UIImage imageWithColor:kRGB(237, 237, 237)];
    tabBarAppearance.shadowColor = kRGB(245, 245, 245);
    self.tabBar.standardAppearance = tabBarAppearance;

} else {
    NSMutableDictionary *selectedAttributes = [[NSMutableDictionary alloc] initWithDictionary:[[UITabBarItem appearance] titleTextAttributesForState:UIControlStateSelected]];
    selectedAttributes[NSForegroundColorAttributeName] = kBlackColor;

    [[UITabBarItem appearance] setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: kBlackColor} forState:UIControlStateNormal];

    // 2.1 正常状态下的文字
    NSMutableDictionary *normalAttr = [NSMutableDictionary dictionary];
    normalAttr[NSForegroundColorAttributeName] = [UIColor blackColor];
    normalAttr[NSFontAttributeName] = [UIFont systemFontOfSize:10];

    // 2.2 选中状态下的文字
    NSMutableDictionary *selectedAttr = [NSMutableDictionary dictionary];
    selectedAttr[NSForegroundColorAttributeName] = [UIColor blackColor];
    selectedAttr[NSFontAttributeName] = [UIFont systemFontOfSize:10];

    // 2.3 统一设置UITabBarItem的文字属性
    UITabBarItem *item = [UITabBarItem appearance];
    // 2.3.1 设置UITabBarItem的正常状态下的文字属性
    [item setTitleTextAttributes:normalAttr forState:UIControlStateNormal];
    // 2.3.2 设置UITabBarItem的选中状态下的文字属性
    [item setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
    self.tabBar.shadowImage = [UIImage imageWithColor:kRGB(237, 237, 237)];
    self.tabBar.backgroundImage = [UIImage imageWithColor:kWhiteColor];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.三个月全然的和平与喜悦 2.轻松顺流快乐的帮助400个家庭告别洗洁精,用上厨小宝,升级公司渠道总监 3.轻而易...
    88429ac7ed3a阅读 1,045评论 0 0
  • 又过去了十天,现在是61.3kg,也是在慢慢地掉秤中,开森~~~ 这段时间其实也没怎么跑步,因为南方下了好长一段时...
    海夕阅读 1,328评论 0 0
  • 读书 √1. Food Rules ×2. 大学之路 演讲 1/2 two jokes √a prepared s...
    泥巴叔叔阅读 1,423评论 0 0
  • 这是我追逐梦想的十年。——孙情悦 看到这一句,不禁鼻头一酸(打了个喷嚏),到底还是老了眼睛里容易进砖头。 刚刚填写...
    海鸥老师阅读 2,383评论 0 0
  • 有一天,儿子很臭美地边照镜子边拨弄自己的头发,嘴里问道:“妈妈,你觉得我是不是真的很帅?”我说:“那当然了,也不看...
    宛新阅读 2,454评论 1 0

友情链接更多精彩内容