【问题标题】:UITabBar images not showing properly on iOS 7UITabBar 图像在 iOS 7 上无法正确显示
【发布时间】:2015-05-18 10:47:34
【问题描述】:

我有这个标签栏在 iOS 8 上正常工作,但在 iOS 7 上没有,有人能指出我正确的方向吗,我使用 JSON 文件在加载第一个屏幕时配置我的 UITabBar。

我必须为 iOS 7 使用不同的图像吗??

是否可以改变de Button的大小来避免这种行为??

iOS 8:

iOS 7:

这是我用来加载图片的代码:

- (void)createTabBar
{
    tabController = [self.storyboard instantiateViewControllerWithIdentifier:@"cCustomTabController"];
    dValue = [dConfiguration objectForKey:@"Buttons"];
    NSMutableArray  *aControllers = [[NSMutableArray alloc] init];
    int i = 0;
    for (NSString* sProperty in dValue) {
        NSString* d = @"Details";
        NetworkStatus internetStatus = [_reachabilityInfo currentReachabilityStatus];
        NSData *itemData = Nil;
        if (internetStatus != NotReachable)
            itemData = [util getSpecificJsonData:[sProperty valueForKeyPath:@"Item"]];
        if(itemData != nil){
            UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
            UIViewController *vcCustom = [aStoryboard instantiateViewControllerWithIdentifier:[util getControllerName:[sProperty valueForKeyPath:@"ViewController"]]];
            UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
            UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
            UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:[sProperty valueForKeyPath:@"Title"] image:imageBtn selectedImage:imageBtnPress];
            UIImage * iSelected = imageBtnPress;
            iSelected = [iSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [tab setSelectedImage:iSelected];
            tab.tag = i;
            if([[sProperty valueForKeyPath:@"Title"] isEqualToString:@"Notificaciones"])
                tab.badgeValue=[sProperty valueForKeyPath:@"Badge"];
            [vcCustom setTabBarItem:tab];
            [vcCustom setTitle:[sProperty valueForKeyPath:@"Title"]];
            [aControllers insertObject:navigationController atIndex:i];
            i++;
        }
        else
        {
            UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
            UIViewController *vcCustom = [aStoryboard instantiateViewControllerWithIdentifier:[util getControllerName:[sProperty valueForKeyPath:@"ViewController"]]];
            UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
            UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
            UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:[sProperty valueForKeyPath:@"Title"] image:imageBtn selectedImage:imageBtnPress];
            UIImage * iSelected = imageBtnPress;
            iSelected = [iSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [tab setSelectedImage:iSelected];
            tab.tag = i;
            if([[sProperty valueForKeyPath:@"Title"] isEqualToString:@"Notificaciones"])
                tab.badgeValue=[sProperty valueForKeyPath:@"Badge"];
            [vcCustom setTabBarItem:tab];
            [vcCustom setTitle:[sProperty valueForKeyPath:@"Title"]];
            UINavigationController *navigationController = [[cCustomNavigationController alloc] initWithRootViewController:vcCustom];
            [aControllers insertObject:navigationController atIndex:i];
            i++;
        }


    }
    tabController.delegate = self;
    tabController.viewControllers = aControllers;
    tabController.tabBar.tintColor = [UIColor blackColor];
}

提前致谢。

【问题讨论】:

  • @MacroAntonioUzcategui 我认为您需要分配 2x 图像。试试看告诉我。
  • @AshokLondhe 我的图像是 2 倍,并且在我使用 iOS 8 的所有视网膜设备中都像魅力一样工作,但在 iOS 7 中使用相同的图像和相同的设备时,我得到了这种可怕的行为。 iOS 7 与 iOS 8 中的预期或工作方式不一样

标签: ios ios7 ios8 icons uitabbar


【解决方案1】:

确保使用包含@1x、@2x 和@3x 图像文件的资产目录来支持任何屏幕分辨率。

我个人使用 Mark Bridges 的Asset Catalog Creator 来创建不同的分辨率。

编辑:
导入多分辨率资产目录后,您可以使用[UIImage imageNamed:@"myImage"];或从界面生成器中选择它。 iOS 会自动选择好的。

【讨论】:

  • 我会试试这个,谢谢你的帮助,我会在几天内告诉你这是否有效。我很着急,我用代码解决了它,但我认为你的解决方案会更优雅
  • 好的,期待您的回音!
  • 我已经按照你说的做了,我仍然有同样的问题,唯一有效的是我发布的代码,我要和设计师谈谈,也许有问题在他创建图像的方式中,您发布的应用程序非常有帮助,感谢您的帮助
  • 你的图片真的比标签栏大吗?最佳做法是使用与您希望它们在屏幕上显示的大小完全相同的资源。对于标签栏,它们不应大于 49px。看看那里:iosdesign.ivomynttinen.com
【解决方案2】:

确保使用包含@1x、@2x 和@3x 的资产目录。

示例:
yourimageName@2x~iphone.png for iphone,

  yourimageName@2x~ipad.png for ipad

【讨论】:

    【解决方案3】:

    我在代码中更改了这两行:

    UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
    
    UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
    

    对于这 2 个:

    UIImage *imageBtn = [self changeImageSize:[UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]] scaledToSize:CGSizeMake(30, 30)];
    
    UIImage *imageBtnPress = [self changeImageSize:[UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]] scaledToSize:CGSizeMake(30, 30)];
    

    并添加了改变图片大小的功能:

    - (UIImage *)changeImageSize:(UIImage *)image scaledToSize:(CGSize)newSize {
    
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    

    我不知道这是否正确,但对我来说是有效的。

    感谢大家,祝大家编码愉快。

    【讨论】:

    • 这是一个非常过分的解决方案。看看我的答案以获得干净的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多