我正在开发一款新的应用程序,并且我遇到了一些问题,需要自定义UITabBar并使其在使用@2x图像的iPhone 5和6上运行良好。
在AppDelegate.m文件中的didFinishLaunchingWithOptions方法中,我设置了背景图像和选定项目的图像:
//TABBAR
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tab_bg"]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"icone_home_selecionado"]];
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
然后,按照相同的方法,对于每个项目,我设置了图像和插入:
tabBarItem1.title = nil;
tabBarItem1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
[tabBarItem1 setImage:[[UIImage imageNamed:@"icone_home_teste"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
tabBarItem2.title = nil;
tabBarItem2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
[tabBarItem2 setImage:[[UIImage imageNamed:@"icone_home_teste"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
tabBarItem3.title = nil;
tabBarItem3.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
[tabBarItem3 setImage:[[UIImage imageNamed:@"icone_home_teste"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
tabBarItem4.title = nil;
tabBarItem4.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
[tabBarItem4 setImage:[[UIImage imageNamed:@"icone_home_teste"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
tabBarItem5.title = nil;
tabBarItem5.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
[tabBarItem5 setImage:[[UIImage imageNamed:@"icone_home_teste"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
我的问题与iPhone 5和6使用相同的@2x图像宽度有关,因为iPhone 5具有640px(320pts),而iPhone 6具有750px(375pts),所以我决定创建名为“icone_home_selecionado@2x.png”的selectedIndicatorImage,其宽度大小为150px。
因为我有5个UITabBarItem,所以750/5 = 150px(每个项目)。
图像icone_home_selecionado@2x.png(150px x 96px):
![icone_home_selecionado@2x.png (150px x 96px)](https://istack.dev59.com/8UVX8.webp)
![enter image description here](https://istack.dev59.com/UykDh.webp)
![enter image description here](https://istack.dev59.com/cUwTM.webp)
我的@2x图像有150px,但由于我应该为iPhone 5和6使用@2x图像,所以我该如何处理此情况以适合UITabBarItem中的图像?如果我有一个150px的图像(为6)和另一个128px的图像(为5),它似乎只会起作用。
是否有任何解决方案可以使用相同的@2x图像,或者我需要编写代码来识别屏幕大小,然后选择哪个图像?