要更改iOS 7中未select的UITabBar图标的颜色?
我知道这个问题早就被问到了,但是我仍然没有在互联网上find解决scheme。
我提到了以下几个职位:
如何更改iOS 7中tabBarItems的文本和图标颜色? 只能使用tintColor
更改选定的图标颜色。
如何更改iOS 7中未选中的标签栏项目的颜色? 在这里他们写了自己从UIViewinheritance的GozTabBar
类
当它处于未选中状态时,我想更改UITabBar
图标的默认灰色。
任何帮助将不胜感激。 提前致谢。
我假设你不想使用tintColor改变颜色? 另一个select是使用两张完全一样的图像,但颜色不同。 一个图像是选定的选项卡,另一个是未选中的。
在你的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;
如果您已经使用Storyboardconfiguration了标签栏图像,只需在第一个视图的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]; } }
[[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]
将iOS的UIControlStateHighlighted更改为UIControlStateSelected
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: titleHighlightedColor, NSForegroundColorAttributeName, nil] forState:UIControlStateSelected]