我尝试更改标签栏项目图片的颜色,为此我使用下一个代码:
// Generate a tinted unselected image based on image passed via the storyboard.
for (UIViewController *vc in tabBarController.viewControllers) {
UITabBarItem *item = vc.tabBarItem;
UIImage *image = item.image;
UIImage *imageSel = [image imageWithColor:selectedColor];
UIImage *imageUnsel = [image imageWithColor:unselectedColor];
// Next is not working to set unselected image!
// But setFinishedSelectedImage does.
//item.selectedImage = imageSel;
item.image = imageSel;
//[item setFinishedSelectedImage:imageSel withFinishedUnselectedImage:imageUnsel];
}
UITabBarItem *item = tabBarController.moreNavigationController.tabBarItem;
UIImage *image = [UIImage imageNamed:@"menu-more"];
UIImage *imageSel = [image imageWithColor:selectedColor];
UIImage *imageUnsel = [image imageWithColor:unselectedColor];
item.image = imageSel;
// [item setFinishedSelectedImage:imageSel withFinishedUnselectedImage:imageUnsel];
imageWithColor:是UIImage扩展,使用原始图像的Alpha值生成带颜色的图像。
第一次执行代码时,一切都很好。
更改颜色后(调用上面的代码),所有标签栏项目都显示在标签栏的左侧,带有文字。发生在模拟器和设备(iPhone + iPad)中。这是一个错误吗?
答案 0 :(得分:0)
您可以使用故事板本身将图像设置为tabbaritem。要将颜色更改为选定的tabbaritem,无需编写任何for循环。你可以使用下面的行。
[[UITabBar appearance] setTintColor:[UIColor greenColor]];
答案 1 :(得分:0)
好吧,我想到了修复。您需要从头开始创建UITabBarItem并将图像设置在那里,而不是设置UITabBarItem的图像,如下所示:
for (UIViewController *vc in tabBarController.viewControllers) {
UITabBarItem *newItem = [[UITabBarItem alloc] initWithTitle:someTitle image:someImageUnsel selectedImage:someImageSel];
vc.tabBarItem = newItem;
}
UITabBarItem *newItem = [[UITabBarItem alloc] initWithTitle:someTitle image:someImageUnsel selectedImage:someImageSel];
tabBarController.moreNavigationController.tabBarItem = newItem;
此解决方案存在问题:在设置tabBarItem后,MoreNavigationController将弹出。常见的Apple,wtf?!
如果有人能提供更好的解决方案,请做...