19

注意:我看到关于 SO 有几个类似的问题,但他们似乎都没有解决我想要更改自定义和系统 UITabBarItems 的未选择外观的具体问题。)

我在 iOS7 中工作。我有一个带有一些按钮的 UITabBar。其中一些是我的按钮,一些是系统按钮。例子:

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];

我可以很容易地设置选定按钮的色调,使用:

[[UITabBar appearance] setSelectedImageTintColor: MY_FAVORITE_COLOR];

我可以将未选择的按钮文本颜色设置为白色:

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

我想要做的是将未选择按钮的图像色调设置为白色。我可以很容易地设置未选择的图像,但这仅适用于我的按钮。我也想为系统按钮执行此操作。也就是说,我希望我的搜索和书签按钮也是未选中的白色。

我很确定如果我尝试重新创建他们的图标来制作自定义按钮,Apple 会抱怨。文档非常具体,我们不应该做任何与 Apple 图标类似的事情。

提示?

谢谢!

编辑:我想要 unselected=white 的原因是,在我设计的背景下,默认的灰色使图标/文本很难看。

4

6 回答 6

12

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

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

self.tabItem.image = tabImage;

“tabItem”是该类的 UITabBarItem Outlet

归功于 Aaron Brager 的回答: UITabBarController unselected icon image tint

于 2014-03-06T16:54:17.657 回答
3

你准备好使用一些私有 API 了吗?

在填充选项卡栏后的某个时间执行此操作:

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

有关私有 API 的通常警告适用:Apple 不希望您这样做;如果它坏了,你可以保留两块;等等

示例代码在 App Store 审查方面并不是特别隐秘。正如所介绍的那样,Apple 会抓住它。添加您最喜欢的隐身技术以适应。

至少适用于 iOS 7.0.1 和 7.1。

于 2014-03-04T18:13:21.120 回答
2

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

这意味着当您配置​​标签栏时,您有以下色调选项:

  • tintColor适用于选定的选项卡
  • unselectedItemTintColor适用于所有未选中的选项卡
于 2016-11-22T12:08:15.343 回答
0

此代码更改选择按钮标题颜色

////为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];

    }
于 2014-01-03T12:35:39.893 回答
0

如果您使用 Storyboard,您还可以同时设置ImageforBar ItemSelected ImageforSelected Bar Item以在tabBar.

于 2016-05-31T10:17:06.487 回答
-4

应该是:

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

摘自:iOS 自定义 UI 系列:TabBar & NavBar:http ://bit.ly/1ahr9Ao

于 2013-10-12T13:38:21.983 回答