iBug
iBug

Reputation: 2352

UITabBarController background image is not set correctly

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

Upvotes: 0

Views: 1034

Answers (2)

baydi
baydi

Reputation: 1003

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)

Upvotes: 3

v2Next
v2Next

Reputation: 159

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:@""];

Upvotes: 0

Related Questions