[iOS 13] UITabBarItem 标题颜色设置适配及 UITabBarAppearance 设置无效问题

在iOS 13升级之后,发现公司APP的UITabBarController上的所有小标题颜色发生异常。

对比之后,发现在iOS 12系统下,这些标题文字的颜色仍然是设置的红色,而在iOS 13中,表现为常见的系统默认蓝。

原来的写法:

viewController.tabBarItem.title = title;
[viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:246.0 / 255.0 green:92.0 / 255.0 blue:92.0 / 255.0 alpha:1.0]} forState:UIControlStateSelected];

经过Google和查看官方文档,在iOS 13中,Apple将设置UITabBarItem的文字属性的任务,交给了13中新添加的属性UITabBarItem.standardAppearance。

下面是适配iOS 13的写法:

viewController.tabBarItem.title = title;
if (@available(iOS 13.0, *)) {
    UITabBarAppearance *appearance = [UITabBarAppearance new];
    // 设置未被选中的颜色
    appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    // 设置被选中时的颜色
    appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor colorWithRed:246.0 / 255.0 green:92.0 / 255.0 blue:92.0 / 255.0 alpha:1.0]};
    
    viewController.tabBarItem.standardAppearance = appearance;
} else {
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:246.0 / 255.0 green:92.0 / 255.0 blue:92.0 / 255.0 alpha:1.0]} forState:UIControlStateSelected];
}

亲测有效。

---------------------------------------------------------------------------------------------------------------

2019.10.31 补充

经网友@会飞的蜗牛up 提醒,在iOS 13.1版本中,通过设置UITabBarItem.standardAppearance修改UITabBarItem标题颜色的方法在有些情况下并不会生效。

请看下面的测试代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 初始化第一个VC
    UIViewController *viewControllerA = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewControllerA"];
    viewControllerA.tabBarItem.title = @"VC A";
    viewControllerA.tabBarItem.image = [UIImage imageNamed:@"Home"];
    
    // 初始化第二个VC
    UIViewController *viewControllerB = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewControllerB"];
    viewControllerB.tabBarItem.title = @"VC B";
    viewControllerB.tabBarItem.image = [UIImage imageNamed:@"Home"];
    
    // 初始化TabBarController
    UITabBarController *tabBarController = [UITabBarController new];
    
    // 初始化Window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    // ----------- 第一种添加方式:appearance 设置生效 ------------
    [tabBarController addChildViewController:viewControllerA];
    [tabBarController addChildViewController:viewControllerB];

    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
    // -----------------------
    
    // ------------ 第二种添加方式:appearance 设置无效 -----------
//    self.window.rootViewController = tabBarController;
//    [self.window makeKeyAndVisible];
//
//    [tabBarController addChildViewController:viewControllerA];
//    [tabBarController addChildViewController:viewControllerB];
    // -----------------------
    
    if (@available(iOS 13.0, *)) {
        // 修改设置
        UITabBarAppearance *appearance = [UITabBarAppearance new];
        // 设置未被选中的颜色
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor], NSBackgroundColorAttributeName: [UIColor redColor]};
        // 设置被选中时的颜色
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor greenColor]};

        viewControllerA.tabBarItem.standardAppearance = appearance;
        viewControllerB.tabBarItem.standardAppearance = appearance;
    }
    
    return YES;
}

对比图:

两种添加方式的对比图

两种添加方式的差别在于,第一种先把UIViewController添加至UITabBarController,然后显示UITabBarController,第二种则先显示UITabBarController,再在UITabBarController上添加UIViewController。效果如同对比图中所示,两种方式都能在UITabBarController添加两个UIViewController,但是第二种方式并不能使UITabBarAppearance设置生效。

难道是第二种方式没有设置成功,不能改变UITabBarItem的UITabBarAppearance?

页面调试截图

通过页面调试,查看UIViewController中UITabBarItem的UITabBarAppearance属性,与设置一致——normal为红底绿字,selected为绿底红字。看来,UITabBarItem的UITabBarAppearance的确被更改了,仅仅只是设置未生效而已。

重新去看了一下UITabBarAppearance和UIBarAppearance的官方文档:

UITabBarAppearance

Overview

After creating a UITabBarAppearance object, use the methods and properties of this class to specify the appearance of items in the tab bar. Use the inherited properties from UIBarAppearance to configure the background and shadow attributes of the tab bar itself.

UIBarAppearance

Overview

UIBarAppearance object contains the common traits shared by navigation bars, tab bars, and toolbars. When configuring a specific type of bar, you usually instantiate the appropriate bar appearance subclass. However, you may also create a UIBarAppearance object, configure its properties, and use it to create new bar appearance objects in your app.

文档中说明了,在创建一个对象后,设置的对象属性时,就需要初始化并设置一个UIBarAppearance或其子类来改变该对象的外观属性。然而,文档中并没有说明,在何时设置UIBarAppearance无效。

推测,UIBarAppearance的属性只会在某一阶段被读取,过了这个阶段后,即使多次修改UIBarAppearance的属性,也不会使最终的界面显示效果发生变化。

如有错误或者补充,请大佬指出。谢谢!

参考资料:https://blog.csdn.net/dreams_deng/article/details/80465322

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值