UITabBar not showing selected item images in ios 7

2020-01-27 00:27发布

The icons show fine in ios 6 but not in ios 7. I'm setting the selected state in the viewController viewDidLoad method. When the user selects a tab bar item the image disappears. Here is my code:

UITabBar *tabBar = self.tabBarController.tabBar;
if ([UITabBar instancesRespondToSelector:@selector(setSelectedImageTintColor:)]) {
    [self.tabBarController.tabBar setSelectedImageTintColor:[UIColor whiteColor]];
}
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
[item0 setTitle:@"Home"];
[item1 setTitle:@"Calendar"];
[item2 setTitle:@"News"];
[item3 setTitle:@"My Events"];
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"homeIconSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home2.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"Calendar"] withFinishedUnselectedImage:[UIImage imageNamed:@"CalendarIconSelected"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"NewsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"News"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:@"EventsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"Events"]];
[item1 imageInsets];
[item2 imageInsets];
[item3 imageInsets];

20条回答
看我几分像从前
2楼-- · 2020-01-27 00:50

I had the same issue. But working with StoryBoards prevented me from changing anything in the code. Leaving the image empty in the storyboard removed this obstacle for me. However putting the initWithTitle in the viewWillAppear method of the tab's viewcontroller gave me odd behaviour. First getting the selected image required an additional click and images did not show up for the non-initial tabs.

For me fixing this was adding the following code to the AppDelegate in the DidFinishLoadingWithOptions (similar to 132206 and Amitabha):

NSArray * vcs = [(UITabBarController*)self.window.rootViewController viewControllers];
UIViewController *tab0 = [[(UINavigationController*)[vcs objectAtIndex:0] viewControllers] objectAtIndex:0];
tab0.title = NSLocalizedString(@"Time", nil);
tab0.tabBarItem =  [[UITabBarItem alloc] initWithTitle:tab0.title image:[UIImage imageNamed:@"Recents.png"] selectedImage:[UIImage imageNamed:@"RecentsSolid.png"]];
UIViewController *tab1 = [[(UINavigationController*)[vcs objectAtIndex:1] viewControllers] objectAtIndex:0];
tab1.title = NSLocalizedString(@"Expense", nil);
tab1.tabBarItem = [[UITabBarItem alloc] initWithTitle:tab1.title image:[UIImage imageNamed:@"Euro.png"] selectedImage:[UIImage imageNamed:@"EuroSolid.png"]];
查看更多
地球回转人心会变
3楼-- · 2020-01-27 00:52

None of the answers worked for me - I am using MonoTouch but if you set the TintColor property of the UITabBar itself that will highlight the selected image with that color. In obj c it may be setTintColor function.

查看更多
孤傲高冷的网名
4楼-- · 2020-01-27 00:52

I had the same problem with Xcode 6.0.1 (6A317), seems to be a bug with Interface builder. However, i've managed to solve it by leaving selected image empty in interface builder then at each viewDidLoad in my view controllers i did insert:

[self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"imagename-selected"]];

it works well now, showing my selectedImage and with global tint mask.

查看更多
beautiful°
5楼-- · 2020-01-27 00:53

Based on the answer from 132206, I made this method for AppDelegate, called from application:didFinishLaunchingWithOptions: with:

[self configureSelectedImages];

It obviously requires a strict naming convention for your tab images, but can also be re-used without editing. To state the obvious - name your selected tab bar images TabbarXXXSelected, where XXX equals the title of the tab bar item.

- (void)configureSelectedImages
{
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;

    for (UITabBarItem *tabBarItem in [tabBar items]) {
        NSString *selectedImage = [NSString stringWithFormat:@"Tabbar%@Selected", [tabBarItem title]];
        (void)[tabBarItem initWithTitle:[tabBarItem title] image:[tabBarItem image] selectedImage:[UIImage imageNamed:selectedImage]];
    }
}
查看更多
家丑人穷心不美
6楼-- · 2020-01-27 00:53

In your first view controller's .h file, I added the following: @property (weak, nonatomic) IBOutlet UITabBarItem *mapViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *profileViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *notificationViewTabBarItem;

(note that the mapViewTabBarItem was linked by ctrl dragging the actual tab bar item into the list of property declarations at the top of the .h file)

Next, in the same view controller's .m file in the viewDidLoad, add the following:

self.tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
_mapViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-map-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
self.tabBarItem.image = [[UIImage imageNamed:@"@2x-map-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

_profileViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:1];
_profileViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-profile-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_profileViewTabBarItem.image = [[UIImage imageNamed:@"@2x-profile-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

_notificationViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:2];
_notificationViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-notifications-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_notificationViewTabBarItem.image = [[UIImage imageNamed:@"@2x-notifications-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
查看更多
小情绪 Triste *
7楼-- · 2020-01-27 00:54

Try this:

UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-Small-50.png"] tag:100];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"image-50.png"] tag:200];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-clip-50.png"] tag:300];
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-color-50.png"] tag:400];
UITabBarItem *item5 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-lock-50.png"] tag:500];

[item1 setSelectedImage:[[UIImage imageNamed:@"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item2 setSelectedImage:[[UIImage imageNamed:@"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item2 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item3 setSelectedImage:[[UIImage imageNamed:@"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item3 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item4 setSelectedImage:[[UIImage imageNamed:@"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item4 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item5 setSelectedImage:[[UIImage imageNamed:@"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item5 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

item1.image = [[UIImage imageNamed:@"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

item2.image = [[UIImage imageNamed:@"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

item3.image = [[UIImage imageNamed:@"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item4.image = [[UIImage imageNamed:@"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item5.image = [[UIImage imageNamed:@"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
查看更多
登录 后发表回答