简体   繁体   English

如何更改选中和未选中的标签栏图像颜色

[英]How to change the tab bar image color for selected and unselected

I'm trying to change the color of a tab bar item in iOS app. 我正在尝试在iOS应用中更改选项卡栏项目的颜色。

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. 我在AppDelegate中添加了此代码,该代码只是更改了文本的颜色。

 [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 或者,您可以在情节提要中进行设置,选择特定的标签项,然后参见下文

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM