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
Change tintColor of unselected UITabBarController item title and background image
提问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
回答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
Image
forBar Item
andSelected Image
forSelected Bar Item
to get unaltered image in tabBar.
如果您使用的是 Storyboard,您还可以同时设置
Image
forBar Item
和Selected Image
forSelected Bar Item
在 tabBar 中获取未更改的图像。
Alternatively in Assets
catalog, you can select Render As
: Original Image
in the attributes of your image (View
> Utilities
> Show Attributes Inspector
or shortcut ??4
(Option + Command + 4))
或者在Assets
目录中,您可以选择Render As
:Original Image
在图像的属性中(View
> Utilities
>Show Attributes Inspector
或快捷方式??4
(Option + Command + 4))