【问题标题】:Change color of inactive tab-bar icons更改非活动标签栏图标的颜色
【发布时间】:2014-09-16 05:35:58
【问题描述】:

我尝试更改标签栏项目的颜色,因为它在非活动状态下始终为灰色,在活动状态下始终为蓝色。 因此,经过一番搜索后,我尝试将这段代码编写为 Tab bar 的所有 ViewControllers

 self.tabBarItem.selectedImage = [[UIImage imageNamed:@"TabBarItemMenu_tabbed.png"]
                                     imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.tabBarItem.image = [[UIImage imageNamed:@"TabBarItemMenu.png"]
                             imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

但这对我没有帮助,而且我总是得到

【问题讨论】:

标签: ios uiimage uitabbarcontroller


【解决方案1】:

您还可以这样做:

设置标签栏tintColor属性设置选中图标的颜色

self.tabBar.tintColor = [UIColor redColor];

然后你可以使用文本属性给文本重新着色

for (UITabBarItem *item in self.tabBar.items) {
    NSDictionary *normalState = @{UITextAttributeTextColor : [UIColor colorWithWhite:1.000 alpha:1.000],
                              UITextAttributeTextShadowColor: [UIColor clearColor],
                              UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]};
    [item setTitleTextAttributes:normalState forState:UIControlStateNormal];

    NSDictionary *selectedState = @{UITextAttributeTextColor : [UIColor redColor],
                                UITextAttributeTextShadowColor: [UIColor clearColor],
                                UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]};
    [item setTitleTextAttributes:selectedState forState:UIControlStateHighlighted];
}

// 编辑

由于 iOS7 不推荐使用上层代码,这里更新:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor whiteColor], NSForegroundColorAttributeName,
                                                   nil] forState:UIControlStateNormal];

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor redColor], NSForegroundColorAttributeName,
                                                   nil] forState:UIControlStateSelected];

【讨论】:

  • 那么如何改变非活动图像的色调?
猜你喜欢
  • 1970-01-01
  • 2014-05-11
  • 2013-06-11
  • 2017-02-03
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多