【问题标题】:Get the color of uitabbaritem with unselected state?获取未选中状态的 uitabbaritem 的颜色?
【发布时间】:2016-04-14 16:21:51
【问题描述】:

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

提前致谢,

JC

【问题讨论】:

  • 你是怎么设置颜色的?

标签: objective-c uicolor uitabbaritem


【解决方案1】:

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

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);
}

【讨论】:

    【解决方案2】:
    UIColor *col = [UITabBar appearance].tintColor; 
    UIColor *col2 = [UITabBar appearance].barTintColor;
    

    这样你可以得到标签栏的bartintColortintColor。我认为tintColor 是您未选择的颜色。试试这个。希望这会有所帮助:)

    更新:

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

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

    【讨论】:

    • 很遗憾,[UITabBar 外观].tintColor 返回 NULL 值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多