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

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

作者: 稻草人12138 | 来源:发表于2019-11-29 17:01 被阅读0次

主要是因为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];
}

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/anpgwctx.html