Search code examples
iosuitabbaruitabbaritem

UITabBarItem Change Image Height and Width


I have a UITabBarItem without a title and only an image. I was wondering how I can change the image size so it can take up the whole UITabBarItem.

I searched all over the internet but found nothing.

I tried:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_Storyboard" bundle:nil];
UITabBarController *tabBarController = [storyBoard instantiateViewControllerWithIdentifier:@"tab"];
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];

tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);

but nothing changed.

Thank you!


Solution

  • from apple documentation the maximum size of a picture on the TabBar are 30x30 (60x60 for the retina display).

    I do not think it's possible to take the entire height of the TabBar without strecth the image. I think the best solution is to center the image in the TabBar using imageInset

    tabBarItem1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    

    enter image description here

    otherwise you can play with this imageInset and strecth the image like in the screenshot

    tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);
    

    enter image description here

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  {
    
    
     UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    
     UITabBar *tabBar = tabBarController.tabBar;
     UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
     UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
     UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
    
     //add image to tabbarItems
    
     tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);
    }