Search code examples
iosios7ios8iconsuitabbar

UITabBar images not showing properly on iOS 7


I have this Tab Bar working properly on iOS 8 but not on iOS 7 can someone point me in the right direction, i use a JSON file to configure my UITabBar when it loads the first screen.

Do I have to use different images for iOS 7??

It is possible to change the size of de Button to avoid the behavior??

iOS 8:

enter image description here

iOS 7:

enter image description here

This is the code I use to load the images:

- (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];
}

Thanks in advance.


Solution

  • I have changed in my code this 2 lines:

    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"]]];
    

    For this 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)];
    

    And added this function to change the size of the image:

    - (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;
    }
    

    I don't know if this is correct but for me is working.

    Thanks to all and happy coding.