我知道此问题也已在早些时候提出过,但我仍然无法在互联网上搜索解决方案。
我提到了以下帖子:
How can I change the text and icon colors for tabBarItems in iOS 7?
仅能使用tintColor
更改所选图标颜色。
How to change the color of unselected tab bar items in iOS 7? 在此,他们编写了自己的GozTabBar
类继承自UIView
我想更改UITabBar
图标处于未选中状态时的默认灰色。
任何帮助都将受到高度赞赏。提前谢谢。
答案 0 :(得分:41)
我假设您不想使用tintColor更改颜色?另一种选择是使用两个看起来完全相同但颜色不同的图像。一个图像是选定的选项卡,另一个图像是未选中的。
在AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
函数中,试试这个。
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
// repeat for every tab, but increment the index each time
UITabBarItem *firstTab = [tabBar.items objectAtIndex:0];
// also repeat for every tab
firstTab.image = [[UIImage imageNamed:@"someImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
firstTab.selectedImage = [[UIImage imageNamed:@"someImageSelected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
编辑:对于那些没有标签栏控制器作为其根视图控制器的人,可以像这样获取控制器,其余代码是相同的。
UITabBarController *tabBarController = self.tabBarController;
答案 1 :(得分:12)
如果您已使用Storyboard配置标签栏图像,只需在第一个视图的ViewDidLoad中调用此方法:
-(void) configTabBar
{
UITabBarController *tabBarController = [self tabBarController];
UITabBar *tabBar = tabBarController.tabBar;
for (UITabBarItem *tab in tabBar.items) {
tab.image = [tab.image imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
tab.selectedImage = [tab.image imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
}
}
答案 2 :(得分:2)
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:252/255.0 green:218/255.0 blue:49/255.0 alpha:1.0]];
tabBarItem1.image = [[UIImage imageNamed:@"home_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1.selectedImage = [UIImage imageNamed:@"home_icon_selected.png"];
[[UITabBar appearance] setBackgroundColor:[UIColor colorWithRed:15/255.0 green:85/255.0 blue:160/255.0 alpha:1.0]];
// Change the title color of tab bar items
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:252/255.0 green:218/255.0 blue:49/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, NSForegroundColorAttributeName,
nil] forState:UIControlStateHighlighted]
答案 3 :(得分:1)
将UIControlStateHighlighted更改为iOS8的UIControlStateSelected
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, NSForegroundColorAttributeName,
nil] forState:UIControlStateSelected]