如何设置UITabBarItem的未选色调,***包括系统项目***(iOS7)

注:我看到有几个类似的问题,但他们都没有看到我想改变定制和系统UITabBarItems的未选定外观的具体问题。)

我正在iOS7中工作。 我有一个button的UITabBar。 其中一些是我的button,一些是系统button。 例:

UITabBarItem *searchButton = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemSearch tag: navSearchItem]; UITabBarItem *bookMkButton = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemBookmarks tag: navBookmarksItem]; UITabBarItem *homeButton = [[UITabBarItem alloc] initWithTitle: @"Home" image: [self tabBarImageNamed: @"home-tab"] tag: navHomeItem]; UITabBarItem *setingButton = [[UITabBarItem alloc] initWithTitle: @"Settings" image: [self tabBarImageNamed: @"settings-tab"] tag: navSettingsItem]; navTabBar.items = @[ searchButton, homeButton, bookMkButton, setingButton]; 

我可以很容易地设置选定的button色调,与:

 [[UITabBar appearance] setSelectedImageTintColor: MY_FAVORITE_COLOR]; 

我可以将未选定的button文字颜色设置为白色:

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

我想要做的是将UNSELECTEDbutton的图像色调设置为白色。 我可以很容易地设置未选中的图像,但只适用于我的button。 我也想为系统button做这个。 也就是说,我希望我的search和书签button也是未选中的白色。

我很确定苹果会抱怨,如果我尝试重新创build自定义button的图标。 文档非常具体,我们不应该做任何与苹果图标类似的东西。

提示?

谢谢!

编辑:我想要取消select=白色的原因是,在我devise的背景下,默认的灰色使图标/文字在眼睛上很难。

我成功地将一个自定义的未选中的UITabBarItem图像设置为白色的唯一方法是使用以下(白色)图像并加载特定的渲染模式

 UIImage *tabImage = [[UIImage imageNamed:@"white_tab_item"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; self.tabItem.image = tabImage; 

“tabItem”是类的UITabBarItem Outlet

积分去Aaron Brager的答案: UITabBarController未选中的图标图像色调

你有一些私人的API使用?

在填充标签栏之后的某个时候执行此操作:

 for (UIView *v in self.tabBar.subviews) if ([NSStringFromClass(v.class) isEqual:@"UITabBarButton"]) [v performSelector:@selector(_setUnselectedTintColor:) withObject:[UIColor whiteColor]]; 

关于私人API的常见警告适用:苹果宁愿不要这样做; 如果它打破,你得保持两件; 等等

示例代码在App Store审查方面并不特别隐秘。 正如所介绍的,苹果将抓住它。 添加您最喜爱的隐身技术,以适应。

至less在iOS 7.0.1和7.1上运行。

在iOS 10中,UITabBar公开unselectedItemTintColor ,您可以使用它指定未选项目的色调。 有关更多信息,请参阅文档 。

这意味着当你configuration一个标签栏,你有以下的色彩选项:

  • tintColor适用于选定的选项卡
  • unselectedItemTintColor适用于所有未选中的选项卡

这段代码改变selectbutton的标题颜色

////为ios7定义这些代码

  #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){ NSLog(@"ios77777"); [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f], NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateNormal]; } 

如果您正在使用Storyboard,则还可以同时设置Image for Bar ItemSelected Image Bar Item Selected Image以在tabBar获取未改变的图像。

应该是:

 [[UITabBar appearance] setTintColor:[UIColor whiteColor]] 

取自:iOS自定义用户界面系列:TabBar&NavBar: http : //bit.ly/1ahr9Ao