ios 更改未选择的 UITabBarController 项目标题和背景图像的 tintColor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26551458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:19:39  来源:igfitidea点击:

Change tintColor of unselected UITabBarController item title and background image

iosios8uitabbaritemtintcolorbartintcolor

提问by SleepsOnNewspapers

How can I change the tintColor of an unselected UITabBarItem title and background image iOS 8?

如何更改未选择的 UITabBarItem 标题和背景图像 iOS 8 的 tintColor?

The default color for an unselected state is a light gray color, but it does not show on my darkish shade UITabBar background

未选中状态的默认颜色是浅灰色,但它不会显示在我的暗色 UITabBar 背景上

I'd like my unselected state to have a color of [UIColor blackColor]

我希望我的未选中状态的颜色为 [UIColor blackColor]

Inside my app delegate didfinishlaunchingwithoptions: I have

在我的应用程序委托中 didfinishlaunchingwithoptions:我有

UIImage *deselectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
UIImage *selectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
e.tabBarItem =  [[UITabBarItem alloc] initWithTitle:@"Profile" image:deselectedE selectedImage:selectedE];
[[UITabBar appearance] setTintColor:[UIColor blackColor]];

回答by SleepsOnNewspapers

Figured it out!

弄清楚了!

Use this to change the color of the text:

使用它来更改文本的颜色:

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] }
                                         forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
                                         forState:UIControlStateSelected];

And make sure that image rendering mode is set to ORIGINAL for the images

并确保图像渲染模式设置为原始图像

UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

回答by TheSD

In your AppDelegate.m inside of application didFinishLaunchingWithOptions:use the following code:

在你的 AppDelegate.m 里面application didFinishLaunchingWithOptions:使用下面的代码:

//unselected icon tint color 
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];

//selected tint color 
[[UITabBar appearance] setTintColor:[UIColor greenColor]];

//text tint color 
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
                                     forState:UIControlStateNormal];

//background tint color 
[[UITabBar appearance] setBarTintColor:[UIColor blueColor]];

回答by Bhavuk Jain

You can also render the image as original from the attributes inspector for the asset file without writing any code

您还可以从资产文件的属性检查器中将图像渲染为原始图像,而无需编写任何代码

enter image description here

在此处输入图片说明

回答by Hugues BR

You can also set it up directly in Storyboard... Check my answer here: How to set UITabBarItem's unselected tint, ***including system items*** (iOS7)

您也可以直接在 Storyboard 中进行设置...在这里查看我的答案: 如何设置 UITabBarItem 的未选择色调,***包括系统项目***(iOS7)

If you're using Storyboard you can also set both Imagefor Bar Itemand Selected Imagefor Selected Bar Itemto get unaltered image in tabBar.

如果您使用的是 Storyboard,您还可以同时设置ImageforBar ItemSelected ImageforSelected Bar Item在 tabBar 中获取未更改的图像。

Alternatively in Assetscatalog, you can select Render As: Original Imagein the attributes of your image (View> Utilities> Show Attributes Inspectoror shortcut ??4(Option + Command + 4))

或者在Assets目录中,您可以选择Render AsOriginal Image在图像的属性中(View> Utilities>Show Attributes Inspector或快捷方式??4(Option + Command + 4))