Search code examples
iosobjective-cuitabbarcontrolleruitabbaruitabbaritem

Icon images not showing on tabbar in iOS 7?


I am going one page to another and that another page have UITabbarcontroller.

That have:

  1. Home 2. CutPipe 3. Fitting 4. Flange 5. Setting

Now these five names are showing but I have these five icon images that are not showing and screen showing black and If I do clearcolor then screen showing grey.

I also want how to control height of UITabbarcontroller.

My problem is that the icon images not showing on tabbar.

I also add background image on UITabbar and add on icon images also. But it is not showing.

Here is my code.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

// Retrieve the last view name
NSInteger firstValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"firstValue"];

if (firstValue == 10) {
    [self method_Tabs];
}
}


-(void)method_Tabs{


// Set the name of the view
[[NSUserDefaults standardUserDefaults] setInteger:11 forKey:@"firstValue"];




  my_Tabbar = [[UITabBarController alloc] init]; // how to control height of my_Tabbar?
 // my_Tabbar.view.frame=CGRectMake(0, 100, 320,30);   // is there any way to control this
// my_Tabbar.view.backgroundColor = [UIColor redColor];

  my_Tabbar.tabBar.barTintColor = [UIColor clearColor];

 self.tabBarController.tabBar.translucent = false;

 // this will give selected icons and text your apps tint color
 my_Tabbar.tabBar.tintColor = [UIColor blueColor];

 [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:13.0f],
                                                    NSForegroundColorAttributeName :    [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
                                                    } forState:UIControlStateNormal];




 Homepage_AdvancePipeOffset  *VC_1 = [[Homepage_AdvancePipeOffset alloc]  initWithNibName:nil bundle:nil];
Apo_Cut_Pipe                *VC_2 = [[Apo_Cut_Pipe alloc] initWithNibName:nil bundle:nil];
Apo_Fitting                 *VC_3 = [[Apo_Fitting alloc] initWithNibName:nil bundle:nil];
Apo_Flange                  *VC_4 = [[Apo_Flange alloc] initWithNibName:nil bundle:nil];
Apo_Setting                 *VC_5 = [[Apo_Setting alloc] initWithNibName:nil bundle:nil];




UINavigationController *rvc_Home     = [[UINavigationController alloc] initWithRootViewController: VC_1];
UINavigationController *rvc_CutPipe  = [[UINavigationController alloc] initWithRootViewController: VC_2];
UINavigationController *rvc_Fitting  = [[UINavigationController alloc] initWithRootViewController: VC_3];
UINavigationController *rvc_Flange   = [[UINavigationController alloc] initWithRootViewController: VC_4];
UINavigationController *rvc_Setting  = [[UINavigationController alloc] initWithRootViewController: VC_5];


rvc_Home.tabBarItem.image    =[UIImage imageNamed:@"home.png"];
rvc_CutPipe.tabBarItem.image =[UIImage imageNamed:@"icon_cut_pipe.png"];
rvc_Fitting.tabBarItem.image =[UIImage imageNamed:@"icon_fittings.png"];
rvc_Flange.tabBarItem.image  =[UIImage imageNamed:@"icon_flanges.png"];
rvc_Setting.tabBarItem.image =[UIImage imageNamed:@"icon_settings.png"];


rvc_Home.tabBarItem.title=@"Home";
VC_1.tabBarController.tabBar.tag = 1;

rvc_CutPipe.tabBarItem.title=@"CutPipe";
VC_2.tabBarController.tabBar.tag = 2;

rvc_Fitting.tabBarItem.title=@"Fitting";
VC_3.tabBarController.tabBar.tag = 3;

rvc_Flange.tabBarItem.title=@"Flange";
 VC_4.tabBarController.tabBar.tag = 4;

 rvc_Setting.tabBarItem.title=@"Setting";
 VC_5.tabBarController.tabBar.tag = 5;


NSArray* controllers = [NSArray arrayWithObjects: rvc_Home,rvc_CutPipe,rvc_Fitting,rvc_Flange, rvc_Setting, nil];


[my_Tabbar setViewControllers: controllers animated:NO];
[self.view addSubview:my_Tabbar.view];
my_Tabbar.selectedIndex=4;
[self.navigationController pushViewController:my_Tabbar animated:YES];

}

Solution

  • I will suggest you to subclass your UITabBarController and set your custom images in tabBarItem in viewDidLoad method .

    Check this code:

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    
    UITabBar *tabBar = self.tabBar;
    
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
    UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
    
    [item0 setFinishedSelectedImage:[UIImage imageNamed:@"search-icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"search-icon"]];
    
    
    [item0 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [item1 setFinishedSelectedImage:[UIImage imageNamed:@"tag-icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"tag-icon"]];
    [item1 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [item2 setFinishedSelectedImage:[UIImage imageNamed:@"add-icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"add-icon"]];
    [item2 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [item3 setFinishedSelectedImage:[UIImage imageNamed:@"profile-icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"profile-icon"]];
    [item3 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    self.selectedIndex=2;
    }
    

    It looks some thing like this to me:

    custom tab bar