I’m developing a new app and I’m facing some problems to customize the UITabBar and make it work (design) great in iPhone 5 and 6 using @2x image.
In the AppDelegate.m, in the didFinishLaunchingWithOptions method, I set the images for background, item selected:
//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]];
And then, in the same method, for each Item I set the image and inset:
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]];
My problem is related iPhone 5 and 6 width using the same @2x image, as the iPhone 5 has 640px (320pts) and iPhone 6 has 750px (375pts), so I decide to create the selectedIndicatorImage called “icone_home_selecionado@2x.png” with width size=150px
Because I have 5 UITabBarItem, so 750/5 = 150px (each item)
Image icone_home_selecionado@2x.png (150px x 96px):
It works really great when run on iPhone 6 as ou can see:
But when test it on iPhone 5, when the item is selected the UITabBarItem area is expanded for the same 150px (as is of the image width) instead of reduce to 128px (it suppose to has this size to fit on iPhone 5) as you can see:
(note the width difference from the first Item to the second item for example, but it happens to all them, it seem that the selected image overlays the UITabBarItem)
My @2x image has 150px, but as I am supposed to use @2x images for iPhone 5 and 6, how can I handle this case to fit the image in UITabBarItem? It seems that it will only work if I have one image 150px (for 6) and another image 128px (for 5)
Is there any solutions using the same @2x image or I need to code to identify that screen size and then select which image?