iOS13 导航栏、tabbar颜色改变问题

4,339 阅读1分钟

iOS13系统更新,导航栏、tabbar文字颜色神奇的改变,记录下解决办法,同时希望能帮到遇到同样问题的小伙伴。亲测可行,如有问题请留言。 代码如下:

tabbar

if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = UITabBarAppearance.new;
        
        UITabBarItemStateAppearance *normal = appearance.stackedLayoutAppearance.normal;
        if (normal) {
            normal.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor grayColor]};
        }
        
        UITabBarItemStateAppearance *selected = appearance.stackedLayoutAppearance.selected;
        if (selected) {
            selected.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor grayColor]};
        }
        self.tabBar.standardAppearance = appearance;
    } else {
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateSelected];
    }

导航栏

UINavigationBar *appearance = [UINavigationBar appearance];
    
    if (@available(iOS 13.0, *)) {
        
        UINavigationBarAppearance *barAppearance = UINavigationBarAppearance.new;
        barAppearance.backgroundColor = [UIColor whiteColor];
        UIBarButtonItemStateAppearance *normal = barAppearance.buttonAppearance.normal;
        if (normal) {
            normal.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor grayColor]};
        }
        
        UIBarButtonItemStateAppearance *highlighted = barAppearance.buttonAppearance.highlighted;
        if (highlighted) {
            highlighted.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor darkGrayColor]};
        }
        
        appearance.standardAppearance = barAppearance;
        
    } else {
        // 设置文字属性
        NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
        textAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
        // UITextAttributeFont  --> NSFontAttributeName(iOS7)
        textAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];            [appearance setTitleTextAttributes:textAttrs];
        
        //设置导航栏的颜色
        [appearance setBarTintColor:[UIColor whiteColor]];
        appearance.translucent = YES;
        
    }