mrosales
mrosales

Reputation: 1568

Custom UITabBarItem without title label

I am trying to create a UITabBar that I will customize using the appearance API. I am stuck on trying to remove the title label from the UITabBarItem and resizing the image to make it centered. If you just delete the title text, there will be empty space below the image and the bottom of the bar.

Does anyone know how to make a UITabBarItem without a title?

Upvotes: 7

Views: 6819

Answers (3)

Carmen
Carmen

Reputation: 6263

Set the title to nil and move the icon

self.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil
                                                image:[[UIImage imageNamed:@"icon_tabbar_inactive.png"]
                                                    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                        selectedImage:[[UIImage imageNamed:@"icon_tabbar_active.png"]
                                                    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

// Move the tabbar icon to the middle of tabbar
self.tabBarItem.imageInsets = UIEdgeInsetsMake(6.0, 0.0, -6.0, 0.0);

Upvotes: 11

Rui LIU
Rui LIU

Reputation: 1

My solution for the same problem:

1, make the bar origin y lower, push the empty space outside the screen.

2, reset the background to ensure it just cover the right position.

Upvotes: 0

CTiPKA
CTiPKA

Reputation: 2974

made same task just today.

Since I can't use full space of tab bar for item image I just change tab bar background when user press any tab.

  1. So the first step is to prepare background images with full size buttons for every tab selected.

  2. Then create custom class and assign it for your tab bar controller

  3. In your custom class set background image for first tab in ViewDidLoad:

    [[self tabBar] setBackgroundImage:[UIImage imageNamed:@"tabShopsActive"]];
    [[self tabBar] setSelectionIndicatorImage:[UIImage imageNamed:@"tabSelection"]];
    
  4. Add background change for every UIController in ViewWillAppear:

    UITabBarController *bar = [self tabBarController];
    [bar.tabBar setBackgroundImage:[UIImage imageNamed:@"tabDiscountsActive"]];
    

So now when user press any tab Tab Bar change background to show correct picture for it.

It is my resulting tab bar when second tab selected:

Tab bar without labels

Upvotes: 7

Related Questions