![](/img/trans.png)
[英]Tab Bar Controller: Icon and Colors when switching to dark mode
[英]Custom tab bar icon colors
我目前正在使用Xcode 5開發面向列表的應用程序。 我在選項卡欄上有自定義色調,在選項卡圖標上有自定義圖像,在選擇選項卡欄時具有自定義色調,但是在找不到圖標時,我找不到如何自定義圖標圖像的色調。 現在,它只是默認的灰色,與我的綠色標簽欄相比,您幾乎看不到它。 我想將標簽欄圖標的圖像和名稱設為白色。
有人知道如何在Xcode 5中設置標簽欄圖標的圖像色調嗎?
您需要將每個選項卡(未選擇)的圖像的渲染模式設置為UIImageRenderingModeAlwaysOriginal
。 因此,在您的應用程序委托中,獲得對選項卡欄的引用,然后遍歷每個選項卡欄項,從而調整圖像模式。
可能有更好的方法來獲取選項卡欄的引用,但是我做了以下工作:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tbc = [sb instantiateInitialViewController];
self.window.rootViewController = tbc;
UITabBar *tb = tbc.tabBar;
然后可以按照以下步驟進行圖像調整:
NSArray *items = tb.items;
for (UITabBarItem *tbi in items) {
UIImage *image = tbi.image;
tbi.selectedImage = image;
tbi.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
您可以嘗試使用此方法為選定的圖標着色:
// Custom the tab bar
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
這可以着色非活動圖標:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];
您可以完全從情節提要中執行此操作,而無需通過添加“用戶定義的運行時屬性”來編寫任何代碼:
嘗試這種方式..它為我工作
應用內委托
- (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
}
奔走
如果可視編輯器中有選項卡欄,則可以在此處進行操作。 選擇選項卡欄,然后在“用戶定義的運行時屬性”中添加屬性:鍵路徑:selectedImageTintColor類型:顏色值:
使用選定的和未選中的圖像設置“自定義”選項卡。 還具有tabbarItem圖像插入位置在中心位置
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);
**在第一個viewcontroller的viewWillAppear方法中。 **
由於不建議使用setFinishedSelectedImage:withFinishedUnselectedImage,因此我使用了Ram S答案的變更版本,方法是替換為:
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ];
與:
[item0 setImage:[[UIImage imageNamed:@"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item0 setSelectedImage:[[UIImage imageNamed:@"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
有關更多信息,請參見iOS7中不推薦使用的UITabBarItem setFinishedSelectedImage : 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.