从 UITabBar 调用 setUnselectedItemTintColor 时应用崩溃

Crash app when call setUnselectedItemTintColor from UITabBar

我已经尝试了下面的两条线,但都导致 [UITabBar setUnselectedItemTintColor:]: unrecognized selector sent to instance

[self.tabBar setUnselectedItemTintColor:[UIColor blackColor]];
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]];

有什么建议吗?

此方法仅在 iOS 10 上可用,因此在以前的版本上会崩溃。您应该在调用之前检查方法的可用性。

 if ([[UITabBar appearance] respondsToSelector:@selector(setUnselectedItemTintColor:)]) {
     [[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]];
 }

试试这个

if ([[UITabBar appearance] respondsToSelector:@selector(setUnselectedItemTintColor:)]) 
    {
        [[UITabBar appearance]setUnselectedItemTintColor:AppYellowColor];
    }
    else
    {
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:AppYellowColor, NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    }