ios 自定义标签栏图标颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18988234/
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
Custom tab bar icon colors
提问by ian
Im currently using Xcode 5 to develop a list oriented app. I have a custom tint for the tab bar, custom images for the tab icons, custom tint for the tab bar's icon images when its selected, but i cannot find how to customize the icon images' tint for when its not selected. Right now its just the default gray which you can barely see in contrast to my green tab bar. I want to make the tab bar icons' images and names white.
我目前正在使用 Xcode 5 开发一个面向列表的应用程序。我有选项卡栏的自定义色调、选项卡图标的自定义图像、选项卡栏图标图像的自定义色调(选中时),但我找不到如何自定义图标图像未选中时的色调。现在它只是默认的灰色,与我的绿色标签栏相比,您几乎看不到。我想让标签栏图标的图像和名称变白。
Does anybody know how to set the tab bar icons' image tint in Xcode 5?
有人知道如何在 Xcode 5 中设置标签栏图标的图像色调吗?
回答by Matthew Burke
You need to set the rendering mode for each tab's (unselected) image to UIImageRenderingModeAlwaysOriginal
. So, in your app delegate, get a reference to the tab bar and then iterate over each tab bar item, adjusting the image modes.
您需要将每个选项卡(未选中)图像的渲染模式设置为UIImageRenderingModeAlwaysOriginal
. 因此,在您的应用程序委托中,获取对标签栏的引用,然后遍历每个标签栏项目,调整图像模式。
There's probably a better way to get a reference to the tab bar, but I did the following:
可能有更好的方法来获取对标签栏的引用,但我执行了以下操作:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tbc = [sb instantiateInitialViewController];
self.window.rootViewController = tbc;
UITabBar *tb = tbc.tabBar;
Then the image adjustment can be done as follows:
然后可以按如下方式进行图像调整:
NSArray *items = tb.items;
for (UITabBarItem *tbi in items) {
UIImage *image = tbi.image;
tbi.selectedImage = image;
tbi.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
回答by Jordan Montel
You can try this to tint selected icon :
你可以试试这个给选定的图标着色:
// Custom the tab bar
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
and this to tint the non active icon :
这是为非活动图标着色的:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];
回答by tebs1200
You can do this purely from the storyboard without writing any code by adding a "User Defined Runtime Attribute":
通过添加“用户定义的运行时属性”,您可以完全从情节提要中完成此操作,而无需编写任何代码:
- Select your UITabViewController in the storyboard
- Open the "Document Outline" and make sure that you select the "Tab Bar" view in the scene.
- Show the "Identity Inspector". You should see a section for "User Defined Runtime Attributes"
- Add the following:
- Key Path: tintColor
- Type: Color
- Value: Select the color you want.
- 在故事板中选择您的 UITabViewController
- 打开“文档大纲”并确保在场景中选择“标签栏”视图。
- 显示“身份检查员”。您应该看到“用户定义的运行时属性”部分
- 添加以下内容:
- 关键路径:tintColor
- 类型:颜色
- 值:选择您想要的颜色。
回答by Bart?omiej Jończy
If you have your tab bar in visual editor, you can do it here. Select tab bar and in "User Defined Runtime Attributes" add attribute: Key Path: selectedImageTintColor Type: Color Value:
如果你在可视化编辑器中有你的标签栏,你可以在这里做。选择标签栏并在“用户定义的运行时属性”中添加属性:键路径:selectedImageTintColor 类型:颜色值:
回答by Tunvir Rahman Tusher
Try this way..it worked for me
试试这种方式..它对我有用
In app delegate
在应用程序委托
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UITabBarController *tabBarController=(UITabBarController*)(self.window.rootViewController);
UITabBar *tabBar=tabBarController.tabBar;
UITabBarItem *tabBarItem1=[[tabBar items] objectAtIndex:0];//first tab bar
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"yourImageSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"yourImageUnselected.png"]];//image should be 30 by 30
}
run and go
跑去
回答by Ram S
Setting Custom Tabbar with selected and non-seleted Image. Also having tabbarItem Image Insets position in center
使用选定和非选定图像设置自定义标签栏。也有 tabbarItem Image Insets 位置在中心
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-2.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-3.png"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-4.png"]];
item0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item3.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
**In viewWillAppear method of first viewcontroller. **
**在第一个视图控制器的 viewWillAppear 方法中。**
回答by dstudeba
Because setFinishedSelectedImage:withFinishedUnselectedImage is deprecated, I used an altered version of Ram S's answer by replacing:
因为 setFinishedSelectedImage:withFinishedUnselectedImage 已被弃用,我使用了 Ram S 答案的更改版本,替换为:
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ];
with:
和:
[item0 setImage:[[UIImage imageNamed:@"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item0 setSelectedImage:[[UIImage imageNamed:@"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
See UITabBarItem setFinishedSelectedImage: deprecated in iOS7for more information.
有关更多信息,请参阅UITabBarItem setFinishedSelectedImage: deprecated in iOS7。