TabBarItem图标不会立即出现在ios 8中

时间:2014-09-23 12:26:08

标签: ios objective-c iphone uitabbarcontroller ios8

我有一个tabBarItem icon的问题。这是我的故事板的一部分的截图。

Storyboard

有一个带有一些按钮的基本viewController。当其中一个按钮被触摸时,会出现一个带有三个标签的tabBarController。在iOS 8 中,tabBarItems的图标最初不会出现。如果我更改了视图控制器,则会出现上一个和当前视图控制器的两个图标。如果我选​​择了第三个标签,那么tabBarItems的所有图标都会出现。这是我的问题的截图:

TabBarController的第一个外观:

1st

如果我更改标签:

2nd

以下是用于将tabBarItems的图标(所有图标均为30x30)设置为TabBarController的第一个viewController的viewDidLoad方法的代码:

[[[self.tabBarController.viewControllers objectAtIndex:0] tabBarItem]  setFinishedSelectedImage:[UIImage imageNamed:@"1d1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"1b1.png"]];

[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]  setFinishedSelectedImage:[UIImage imageNamed:@"2d1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"2b1.png"]];

[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]  setFinishedSelectedImage:[UIImage imageNamed:@"3d1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"3b1.png"]];

以下是适用于iOS 7的屏幕截图:

right

任何人都可以帮我解决这个问题吗?

4 个答案:

答案 0 :(得分:7)

经过几个小时的搜索和阅读不同的答案,包括@ Zala的回答,这部分代码进入了TabBarController的第一个viewController的viewDidLoad,最终发挥了作用。

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{

    UITabBarItem *tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];

    UIImage *unselectedImage = [UIImage imageNamed:@"1b1.png"];
    UIImage *selectedImage = [UIImage imageNamed:@"1d1.png"];

    [tabBarItem setImage: [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [tabBarItem setSelectedImage:[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];


    UITabBarItem *tabBarItem1 = [self.tabBarController.tabBar.items objectAtIndex:1];

    UIImage *unselectedImage1 = [UIImage imageNamed:@"2b1.png"];
    UIImage *selectedImage1 = [UIImage imageNamed:@"2d1.png"];

    [tabBarItem1 setImage: [unselectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [tabBarItem1 setSelectedImage: [selectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    UITabBarItem *tabBarItem2 = [self.tabBarController.tabBar.items objectAtIndex:2];

    UIImage *unselectedImage2 = [UIImage imageNamed:@"3b1.png"];
    UIImage *selectedImage2 = [UIImage imageNamed:@"3d1.png"];

    [tabBarItem2 setImage: [unselectedImage2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [tabBarItem2 setSelectedImage:[selectedImage2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];


    [tabBarItem setTitle:@"Φωτογραφίες"];

    [tabBarItem2 setTitle:@"360° Φωτογραφία"];

    [tabBarItem1 setTitle:@"Βίντεο"];



}
else
{


    [[[self.tabBarController.viewControllers objectAtIndex:0] tabBarItem]  setFinishedSelectedImage:[UIImage imageNamed:@"1d1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"1b1.png"]];


    [[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]  setFinishedSelectedImage:[UIImage imageNamed:@"2d1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"2b1.png"]];
    [[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]  setFinishedSelectedImage:[UIImage imageNamed:@"3d1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"3b1.png"]];

    [[[self.tabBarController.viewControllers objectAtIndex:0] tabBarItem] setTitle:@"Φωτογραφίες"];

    [[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem] setTitle:@"360° Φωτογραφία"];

    [[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem] setTitle:@"Βίντεο"];



}

答案 1 :(得分:5)

setFinishedSelectedImage:withFinishedUnselectedImage:已从iOS 7使用image和[{1}} selectedImage属性弃用。

设置自定义图片:

UITabBarItem

希望这很好。

即使在iOS7中不推荐使用UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0]; if ([self iOS7OrAbove]) { //use UIImageRenderingModeAlwaysOriginal to set the custom image for ios 7 and above. tabBarItem1.selectedImage = [[UIImage imageNamed:@"SelectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; tabBarItem1.image = [[UIImage imageNamed:@"UnselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } else { [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"SelectedImage"] withFinishedUnselectedImage:[UIImage imageNamed:@"UnselectedImage"]]; } ,它在iOS7中也能正常工作,但在8中却没有。

答案 2 :(得分:1)

您也可以通过故事板设置这些图像: http://imgur.com/lmyi1Nw

如果您想以动态的方式使用它,我建议继承UITabBarController并覆盖它的viewWillAppear:

答案 3 :(得分:0)

是的,使用故事板无法做到这一点 - 需要编写代码。

您必须在viewdidload操作中对其进行编码

UITabBar *tabBar = self.tabBar;

UITabBarItem *targetTabBarItem = [[tabbar items] objectAtIndex:0]; // whichever tab-item
UIImage *selectedIcon = [UIImage imageNamed:@"name-of-selected-image.png"];
[targetTabBarItem setSelectedImage:selectedIcon];