简体   繁体   中英

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:

在此处输入图片说明

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM