如何更改iOS7中未选择的tabbaritem颜色?

atr*_*rik 3 objective-c uitabbar ios ios7

在iOS 7之前我使用过

[[UITabBar appearance] setTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)

但是现在它只绘制了所选项目,我已经阅读了一些建议,但我无法完成如何操作,我也使用了它:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"openbookwp4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"openbookwp4.png"]];
Run Code Online (Sandbox Code Playgroud)

这把我想要的图标和我想要的颜色放在一起,但只有在我选择该标签后,例如,当我打开应用程序时,标签看起来正常,但在我按下第二个标签并返回第一个标签后,第二个标签现在有我想要的颜色.没有图像很难解释,但我无法发布图像......

Nik*_* M. 20

此代码适用于iOS 7.

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                    NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
                                                    } forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

根据需要设置前景色.

还要影响未选择的标签栏图标:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], UITextAttributeTextColor, nil]
                                         forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

如果它不起作用,唯一的方法是选择和未选择状态的图像:

// set selected and unselected icons
UITabBarItem *item = [self.tabBar.items objectAtIndex:0];

// this way, the icon gets rendered as it is (thus, it needs to be green in this example)
item.image = [[UIImage imageNamed:@"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:@"selected-icon.png"];
Run Code Online (Sandbox Code Playgroud)

  • 嗨,这只改变文字颜色,我该如何改变图标颜色?谢谢 (4认同)
  • 在iOS7(UITextAttributeTextColor)上不推荐使用第二个代码片段,您可以使用[[UITabBarItem外观] setTitleTextAttributes:@ {NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateNormal]; (2认同)