Reputation: 2437
My way for customizing the UITabbar was working fine in IOS 5 and IOS 6,but in IOS7 the Tabbar did not show any image.
IOS6 Result:
IOS7 Result:
After doing some research i try to fix the existing code,but did not succeed.here is my code which was working fine in ios6
#import <Foundation/Foundation.h>
@interface CustomTabBarItem : UITabBarItem
{
UIImage *selectedImg;
UIImage *unSelectedImg;
}
@property (nonatomic, retain) UIImage *selectedImg;
@property (nonatomic, retain) UIImage *unSelectedImg;
@end
#import "CustomTabBarItem.h"
@implementation CustomTabBarItem
@synthesize selectedImg;
@synthesize unSelectedImg;
-(UIImage *) selectedImage
{
return self.selectedImg;
}
-(UIImage *) unselectedImage
{
return self.unSelectedImg;
}
@end
Now in appDelegate
self.tabBarController.delegate = self;
self.tabBarController.tabBar.frame = CGRectMake(0, self.tabBarController.tabBar.frame.origin.y, self.tabBarController.tabBar.frame.size.width, 49);
for(int i=1;i<=4;i++)
{
CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"" image:nil tag:0];
tabItem.selectedImg=[UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_over_%@.png",i,deviceType]];
tabItem.unSelectedImg=[UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_%@.png",i,deviceType]];
UIEdgeInsets titleInsets = UIEdgeInsetsMake(6.0, 0.0, -6.0, 0.0);
tabItem.imageInsets = titleInsets;
[[self.tabBarController.viewControllers objectAtIndex:i-1] setTabBarItem:tabItem];
[tabItem release];
}
The above code is working fine in in IOS6,after doing some research i did some changes for IOS7
[[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"" image:nil tag:0];
tabItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_over_%@.png",i,deviceType]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_%@.png",i,deviceType]];
but still the result is same , any help will be appreciated thanks.
Upvotes: 1
Views: 4512
Reputation: 861
See my answer at https://stackoverflow.com/a/20007782/1755055 for what I worked out.
I believe there is either a limitation or a bug in using the appearance class property for this in ios7.
Your tab bar item is using the icon image as a template and coloring it with the tint color. What Apple really wants you to do is design Icons for tab bars that are mostly transparent so that they can be used as a template image.
See Bar Button Icons in the MobileHIG document around page 204 for a discussion on designing these.
So to set the selected tab bar item you need to call 'setSelectedImage:' on the 'UITabBarItem' that you can get from UIViewContoller. If your subclass of UIViewController is wrapped by a NavigationController on the tab, you get the tab bar item from that ViewController.
I use storyboards, so I can set the tab image in Interface Builder. The selectedImage property is not available there right now, so you have to set it in code. I did this in each of my main view controllers that appear at the top of navigation controller stacks in each tab.
Your example needs to have the image rendered as it was designed, so you also need to set the rendering mode on the image.
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self.navigationController.tabBarItem setSelectedImage:[[UIImage imageNamed:@"MySelectedIcon.png"]
imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
}
Upvotes: 4
Reputation: 9246
it's not tint, but you can do it with images:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];
Have you tried setting barTintColor on the instance of the tab bar directly, instead of the UIAppearance proxy?
This is a known issue in iOS 7. The tintColor is used for the selected tab image. The selectedImageTintColor is completely ignored. There is no way to tint unselected tab images.
See a discussion on the Apple Developer Forums https://devforums.apple.com/message/851126#851126 about this.
Upvotes: 0