iosipaduitabbarcontrolleruiedgeinsets

UITabbar Images and title are not center aligned in ipad


I am working on application which uses Tabbarcontroller (There are two tabs ) Everything works fine on iphone but on ipad both the tabs comes in center of the screen

I want to show the title and images in center of each tab (considering the tab width is half of the screen)

While searching for these I came across UIEdgeInsetsMake

Adjust iOS TabBar item title to the centre of it but no help with this.

I dont understand where to put this code. I tried to put it in FirstViewController and then in app delegate (one by one ) but nothing works for me

Here is my appDelegete Code :

FirstViewController *firstVC = [[FirstViewController alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:firstVC];
//navController1.title = @"First Tab";
navController1.tabBarItem.image = [UIImage imageNamed:@"first.png"];

SecondViewController *secondVC = [[SecondViewController alloc] init];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondVC];
navController2.title = @"Second Tab";
navController2.tabBarItem.image = [UIImage imageNamed:@"second.png"];

NSArray *array = [[NSArray alloc] initWithObjects:navController1,navController2, nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
 tabBarController.tabBarItem.imageInsets = UIEdgeInsetsMake(0, -50, 0, 50);

tabBarController.viewControllers = array;

[self.window setRootViewController:tabBarController];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;

Can anybody help ?


Solution

  • With reference to this answer I would suggest you to do like this

    1 Get the screen width and divide it by the number of tab bar items you have

    2 Once divided assgin the mean value via setItemWidth to the UITabBar

    CGRect screenSize = [[UIScreen mainScreen] applicationFrame];
    int count  = [array count];
    float width  = screenSize.size.width/count;
    [[UITabBar appearance] setItemWidth:width];