1

I have a simple design for each TabBar item :

  • a background color:

    firstTab width = 1/3 of the screen(left) height = tabBarHeight secondTab width = 1/3 of the screen(in the middle) height = tabBarHeight thirdTab width = 1/3 of the screen(right) height = tabBarHeight

  • a title centered horizontal and vertical of the tabbaritem

I found different articles about how to change the entire tabbar background like this :

UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];

And I found selected images per tabbaritem but that doesn't fill the entire tab :

[[tabBarController.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:@"tabbaritem.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tabbaritem.png"]];
Shishi
  • 601
  • 2
  • 8
  • 27

3 Answers3

2
UITabBarItem *tab1 = [[UITabBarItem alloc] init];
tab1.image = [[UIImage imageNamed:@"home_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab1.selectedImage = [[UIImage imageNamed:@"home_icon_selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
Ramdhas
  • 1,765
  • 1
  • 18
  • 26
  • This has the same result as the second `[[tabBarController.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:@"tabbaritem.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tabbaritem.png"]];` – Shishi May 02 '14 at 06:58
  • this ans fixed your issue? – Ramdhas May 02 '14 at 08:45
2

Better way is create a subClass of UITabBarController

To changeBackGround Color ,

 self.tabBar.backgroundColor = [UIColor redColor];

for tintColor

self.tabBar.barTintColor = [UIColor greenColor];

in ' init ' method itself.

For seetTitle

create an array

//title

titleArray = [[NSMutableArray alloc]initWithObjects:@"Title 1",@"Title 2",@"Title 3", nil];

//image array

NSArray *imageArray = @[image1,image2,image3];



for (int i = 0; i< imageArray.count; i++)
  {
   UINavigationController *nav = [self.viewControllers objectAtIndex:i];
   [nav.tabBarItem setTitle:[titleArray objectAtIndex:i]];

   //You can adjust title position by adjust `titlePositionAdjustment`
   nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -15);

  //for add image on tabbr item for ios 7
  nav.tabBarItem.image = [[imageArray objectAtIndex:i]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

  }

To add image on ios 6

nav.tabBarItem.image = [imageArray objectAtIndex:i];

To cahnge selected image in ios 7

nav.tabBarItem.image = [imageArray objectAtIndex:i];

for ios 6

nav.tabBarItem.selectedImage = [imageArraySelected objectAtIndex:i];

Adjust Image EdgeInset

nav.tabBarItem.imageInsets = UIEdgeInsetsMake(14,0.0, -14,-2);

For add title Attibutes

[nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"Helvetica-Bold" size:14.0f],NSFontAttributeName, nil] forState:UIControlStateNormal];
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • Still not a answer to my question as this sets the whole background color instead of per tabbaritem – Shishi May 02 '14 at 07:16
  • you can put image on tabbar Item – Vineesh TP May 02 '14 at 07:17
  • I know as I said in the question when I do that it doesn't fill the height and width. I would like the background color like this firstTab width = 1/3 height = tabBarHeight secondTab width = 1/3(in the middle) height = tabBarHeight thirdTab width = 1/3(right) height = tabBarHeight – Shishi May 02 '14 at 07:27
0

You can do like this

 UIImage *tabBackground = [[UIImage imageNamed:@"tabbg.png"]
 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

 [[UITabBar appearance] setBackgroundImage:tabBackground];


 UITabBarItem *tabBarItem1 = [tabbar.items objectAtIndex:0];
 UITabBarItem *tabBarItem2 = [tabbar.items objectAtIndex:1];
 UITabBarItem *tabBarItem3 = [tabbar.items objectAtIndex:2];

 tabBarItem1.title = @"one";
 tabBarItem2.title = @"Two";
 tabBarItem3.title = @"Three";

 [tabbar setSelectedItem:[tabbar.items objectAtIndex:0]];
 [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"one.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"one_active.png"]];
 [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"Two".png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Two_active.png"]];
    [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"Three.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Three_active.png"]];
Nisha
  • 354
  • 2
  • 19
  • This will set a background to the whole tabbar instead of tabbaritem – Shishi May 02 '14 at 06:50
  • your tabbar's background image is different and your tabitems image is different right?? one min i will post – Nisha May 02 '14 at 06:51
  • It's just as I explained in the question what I need is tabs with different background colors that's all but the title should also be centered – Shishi May 02 '14 at 06:53
  • than i think you have to use custom tabbar search for custom tabbar it may help you – Nisha May 02 '14 at 07:02
  • [[UITabBar appearance] setTintColor:[UIColor whiteColor]]; try this – Nisha May 02 '14 at 07:07