0

I am trying to add UITabBarController programmatically. Everything is working fine but I am having two issues.

  1. I am setting tabbar background image but it shows a different image which I dont even have in resources. I am using this image as tabbar background image with a green line above :

But it shows another green line at bottom like this:

enter image description here

Here is the code I am using for this:

[self.myTabBarController.tabBar setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tabbar.png" ofType:nil]]];
  1. Another issue is, I am setting tabbar item images using this code:

     MyViewController *myController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
     UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController: myController];
     [myNavController.tabBarItem initWithTitle:@"" image:[UIImage imageNamed:@"ads_inactive.png"] selectedImage:[UIImage imageNamed:@"ads_active.png"]];
    

Images are set but when i try to add title in MyViewController's viewDidLoad using this:

self.title = @"My Ads";

It shows same title on tabbar item too but I dont want any title there.

How I can fix this issue? Thanks

iBug
  • 2,334
  • 3
  • 32
  • 65
  • there's a fix for this that i use, but the problem is that it's going to require subclassing a UIViewController and using this subclass as your parent class for all your viewcontrollers in the naivgationcontrollers of your tabbar, if this isn't something you've done before, that is using subclasses of subclasses, then it's just going to confuse you, but this fixes the title issue, this issue is going to plague you unless yoiu get it right. – Larry Pickles Aug 28 '15 at 07:04
  • if it sounds like you can handle this, then I can help, but it's a lot of code – Larry Pickles Aug 28 '15 at 07:04

2 Answers2

3

The image size is less than the frame size of tab bar , so to cover to frame area background image show two times. You can change it in two ways 1.)Change the image size that will be not the best option 2.) set the content insets of tabBar like (0,0,0,0)

baydi
  • 1,003
  • 6
  • 11
0

Example:

UITabBar *tabBar = self.tabBarController.tabBar;
iterm0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

UITabBarItem *iterm0 = [tabBar.items objectAtIndex:0];
[iterm0 setImage:[[UIImage imageNamed:@"tab1_normal"]
                  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[iterm0 setSelectedImage:[[UIImage imageNamed:@"tab1_selected"]
                          [(UITabBarItem *)[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:@""];
v2Next
  • 159
  • 1
  • 9