[英]how do you change the tint color of an unselected image in a tab bar in iOS?
[英]How to change the tab bar image color for selected and unselected
我正在嘗試在iOS應用中更改選項卡欄項目的顏色。
子視圖控制器嵌入在導航控制器中。
我在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];
在圖像資產內部,如果我將特定圖像渲染為原始圖像,則可以使用,但是選擇顏色不適用於該圖像。
請讓我知道可以做什么。
提前致謝...
嘗試以下代碼:當您要以編程方式進行設置時。
// 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";
或者,您可以在情節提要中進行設置,選擇特定的標簽項,然后參見下文
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.