获取未选中状态的 uitabbaritem 的颜色?

Get the color of uitabbaritem with unselected state?

我更改了未选中状态的uitabbaritem(文本+图像)的颜色。 我想知道是否有办法获得这种颜色?我知道我们可以通过 [UITabBar appearance].selectedImageTintColor 获得选定的颜色,但对于未选定的颜色,我不知道是否可行。

提前致谢,

JC

UIColor *col = [UITabBar appearance].tintColor; 
UIColor *col2 = [UITabBar appearance].barTintColor;

这样就可以得到bartintColortintColor的tab bar。我认为这里 tintColor 是您未选择的颜色。试试这个。希望这会有所帮助:)

更新:

[[UITabBar appearance] setTintColor:[UIColor redColor]];
UIColor *clr =  [UITabBar appearance].tintColor;
self.screenTitleLabel.textColor = clr;

这是将 screenTitleLabel 文本颜色设置为红色。这意味着这将返回我设置的红色。尝试一下。

要找出 UITabBarItem 的实际颜色,即使在使用以下代码之前不调用任何外观 API。它查询视图层次结构并使用第一个和第二个按钮来确定实际的 UIColor。对于 IOS9,它为 selectedColor 提供 "UIDeviceRGBColorSpace 0 0.478431 1 1"(十六进制的#007aff),为 inactiveColor 提供 "UIDeviceWhiteColorSpace 0.572549 1"(十六进制的#929292)。这在未来的版本中当然可能会改变。要为具有 tintColors、外观等设置的项目获取具体颜色,请对实际的 UITabBar 使用 findTabBarLabel()。

static UILabel* findTabBarLabel(UITabBar* tb,NSString* text)
{
  for (UIView* btn in tb.subviews) {
    if (![btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {continue;}
    for (UIView* sv in btn.subviews) {
      if (![sv isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")]) {continue;}
      UILabel* l=(UILabel*)sv;
      if ([text isEqualToString:l.text]) {
        return l;
      }
    }
  }
  return nil;
}

static void retrieveTabBarSystemColors()
{
  UITabBarController* tc=[[UITabBarController alloc] init];
  UITabBarItem* it1=[[UITabBarItem alloc] initWithTitle:@"foo" image:nil tag:1];
  UIViewController* vc1=[[UIViewController alloc] init];
  vc1.tabBarItem=it1;
  UITabBarItem* it2=[[UITabBarItem alloc] initWithTitle:@"bar" image:nil tag:2];
  UIViewController* vc2=[[UIViewController alloc] init];
  vc2.tabBarItem=it2;
  tc.viewControllers=@[vc1,vc2];
  UITabBar* tb=tc.tabBar;
  UILabel* label1=findTabBarLabel(tb,@"foo");
  NSLog(@"Tab bar item active:%@",label1.textColor.description);
  UILabel* label2=findTabBarLabel(tb,@"bar");
  NSLog(@"Tab bar item inactive color:%@",label2.textColor.description);
}