我的tabbar项目图片存在问题。我在每个tabbar项目上设置了一个图像,但是当我运行应用程序时,出现的唯一图像是第一个tabbar项目的图像。在我选择其中一个选项卡之前,其他标签栏项目的图像不会显示。我用来在tabbar项目上设置图像的代码如下:
[self.tabBarItem setImage:[UIImage imageNamed:@"multi30x30.png"]];
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"multi30x30.png"]
withFinishedUnselectedImage:[UIImage imageNamed:@"multi30x30.png"]];
有人可以帮助我吗?
答案 0 :(得分:7)
你可以试试这个:
[[[self.tabBarController.viewControllers objectAtIndex:0] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"LocateIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"LocateIconInactive.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"ProductsIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"ProductsIconInactive.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"NextDeliveryIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"NextDeliveryIconInactive.png"]];
答案 1 :(得分:1)
[[tabBarController.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:@"blabla1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bleble1.png"]];
[[tabBarController.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:@"blabla2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bleble2.png"]];
[[tabBarController.tabBar.items objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:@"blabla3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bleble3.png"]];
答案 2 :(得分:1)
使用此代码: -
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
UITabBarController *tabBarController
= (UITabBarController
*)self.window.rootViewController;
UIImage *selectedImage0 = [UIImage
imageNamed:@"tb_inorganic_selected"];
UIImage *unselectedImage0 = [UIImage imageNamed:@"tb_inorganic_normal"];
UIImage *selectedImage1 = [UIImage imageNamed:@"tb_organic_selected"];
UIImage *unselectedImage1 = [UIImage imageNamed:@"tb_organic_normal"];
UIImage *selectedImage2 = [UIImage imageNamed:@"tb_acid_selected"];
UIImage *unselectedImage2 = [UIImage imageNamed:@"tb_acid_normal"];
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
答案 3 :(得分:0)
创建并返回具有指定标题,未选择图像和所选图像的新项目。 如果未提供selectedImage,则图像将用作未选择和选定的图像。 默认情况下,将根据源图像中的Alpha值自动创建实际未选择和选定的图像。要防止系统着色,请使用UIImageRenderingModeAlwaysOriginal提供图像。可用性 iOS 7 及更高版本。
UIImage *aImage = [UIImage imageNamed:@"a"];
aImage = [aImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *inaImage = [UIImage imageNamed:@"ina"];
inaImage = [inaImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.aController.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"title" image:inaImage selectedImage:aImage];
答案 4 :(得分:0)
我希望ViewController执行此操作。因此,在我看来,最好的解决方案应该是覆盖init方法并在此处设置TabBarItem。
可能你在viewDidLoad中这样做,所以只出现第一个项目。
对不起拼写错误。
答案 5 :(得分:0)
SWIFT 5.4
tabBarController?.viewControllers?[index].tabBarItem.image = UIImage(named: "imageName")