如何更改选定和未选定的标签栏图像颜色 [英] How to change the tab bar image color for selected and unselected
问题描述
我正在尝试更改iOS应用中标签栏项目的颜色。
子视图控制器嵌入在导航控制器中。
我在AppDelegate中添加了此代码改变文本的颜色。
[UITabBarItem.appearance setTitleTextAttributes:
@ {NSForegroundColorAttributeName:[的UIColor colorWithRed:38 / 255.0绿色:54 / 255.0 blue:73 / 255.0 alpha:1.0]}
forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:@ {NSForegroundColorAttributeName:kColorTabbarNavigationBarTint}
forState:UIControlStateSelected];
在图像资源中,如果我将特定图像渲染为原始图像,则可以使用,但是选择颜色不适用于图像。
请告诉我可以做些什么。
谢谢提前...
尝试以下代码:当你想以编程方式设置时。
'pre>
//得到你的tabBarController
//的UITabBarController * tabBarController = self.tabBarController
的UITabBarController * tabBarController =(*的UITabBarController)self.window.rootViewController ;
UITabBar * tabBar = tabBarController.tabBar;
UITabBarItem * tabItem1 = [tabBar.items objectAtIndex:0];
[tabItem1 setSelectedImage:[UIImage imageNamed:@selectimg]];
tabItem1.selectedImage = [[UIImage imageNamed:@selectimg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem1.image = [[UIImage imageNamed:@deselectimg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem1.title = @xxxx;
UITabBarItem * tabItem2 = [tabBar.items objectAtIndex:1];
tabItem2.selectedImage = [[UIImage imageNamed:@selectimg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem2.image = [[UIImage imageNamed:@deselectimg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem2.title = @xxxx;
UITabBarItem * tabItem3 = [tabBar.items objectAtIndex:2];
tabItem3.selectedImage = [[UIImage imageNamed:@selectimg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem3.image = [[UIImage imageNamed:@deselectimg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem3.title = @xxxx;
UITabBarItem * tabItem4 = [tabBar.items objectAtIndex:3];
tabItem4.selectedImage = [[UIImage imageNamed:@selectimg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem4.image = [[UIImage imageNamed:@deselectimg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem4.title = @xxxx;
或者您可以在故事板中设置它,选择特定标签项并查看以下内容
I'm trying to change the color of a tab bar item in iOS app.
The child view controllers are embedded in an navigation controller.
I have added this code in AppDelegate which just changes the color of the text.
[UITabBarItem.appearance setTitleTextAttributes:
@{NSForegroundColorAttributeName : [UIColor colorWithRed:38/255.0 green:54/255.0 blue:73/255.0 alpha:1.0]}
forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : kColorTabbarNavigationBarTint}
forState:UIControlStateSelected];
And inside the image assets,if I make the particular image to be rendered as original it works, but the selection color does not work for the image.
Please let me know what could be done.
Thanks in advance...
Try below code : When you want to set programatically.
// get your TabBarController
//UITabBarController *tabBarController = self.tabBarController
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabItem1 = [tabBar.items objectAtIndex:0];
[tabItem1 setSelectedImage:[UIImage imageNamed:@"selectimg"]];
tabItem1.selectedImage = [[UIImage imageNamed:@"selectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabItem1.image = [[UIImage imageNamed:@"deselectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabItem1.title = @"xxxx";
UITabBarItem *tabItem2 = [tabBar.items objectAtIndex:1];
tabItem2.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabItem2.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabItem2.title = @"xxxx";
UITabBarItem *tabItem3 = [tabBar.items objectAtIndex:2];
tabItem3.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabItem3.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabItem3.title = @"xxxx";
UITabBarItem *tabItem4 = [tabBar.items objectAtIndex:3];
tabItem4.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabItem4.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabItem4.title = @"xxxx";
Or you can set it in storyboard, Select particular tab item and see below
这篇关于如何更改选定和未选定的标签栏图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!