How to set UITabBarItem's unselected tint, ***including system items*** (iOS7)

我的未来我决定 提交于 2019-11-28 20:29:28
thgc

The only way I was successful in setting a custom unselected UITabBarItem image to white was by using the following (white) image and loading it with a specific rendering mode

UIImage *tabImage = [[UIImage imageNamed:@"white_tab_item"] 
                      imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.tabItem.image = tabImage;

"tabItem" being a UITabBarItem Outlet of the class

Credits go to Aaron Brager's answer: UITabBarController unselected icon image tint

Are you up for some private API use?

Do this sometime after the tab bar has been populated:

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

Usual warnings about private API apply: Apple would rather you not do this; if it breaks you get to keep both pieces; etc.

The example code is not particularly stealthy with regard to App Store review. Apple will catch it, as presented. Add your favorite stealth techniques to suit.

Works on at least iOS 7.0.1 and 7.1.

In iOS 10 UITabBar exposes unselectedItemTintColor that you can use to specify the tint of unselected items. See the docs for more.

This means when you are configuring a tab bar you have the following tint options:

  • tintColor applies to the selected tabs
  • unselectedItemTintColor applies to all unselected tabs

this code to change select button title color

////define these code for 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];

    }

If you're using Storyboard you can also set both Image for Bar Item and Selected Image for Selected Bar Item to get unaltered image in tabBar.

Should it be:

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

Taken from: iOS Custom UI Series: TabBar & NavBar: http://bit.ly/1ahr9Ao

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!