Atma
Atma

Reputation: 29767

how to set image for uitabbarcontroller in cocoa code

Hi I am creating a tab bar controller in xcode and not in interface builder. the titles of the views in the tabs set the titles in the tabs but I'm unsure how to set images.

Can anyone help?

Upvotes: 6

Views: 16664

Answers (5)

Ali A. Jalil
Ali A. Jalil

Reputation: 921

SWIFT 5.4

let tabs = tabBarController.viewControllers
let tab1 = tabs[0]
tab1.tabBarItem.image = UIImage(named: "clockicon.png")
let tab2 = tabs[1]
tab2.tabBarItem.image = UIImage(named: "nearest.png")

Upvotes: 0

adMaze
adMaze

Reputation: 21

the UITabBarController does not allow many traits, like colorful images for tab bar items, fonts for the bar items, as well as background image for the Tab Bar. i have found a way around. when one UITabBarController is added to an UITabBarController (not the app delegate) through the xib, there is also a UIView file for the UITabBarController (with the name 'view'). add image views with your images for the Tab Bar as well as Tab bar Items, and labels and whatever you wish, such that they fall right under the frame of the Tab Bar. now we have to make the tab bat invisible but still workable. reduce the alpha value for the Tab Bar of the UITabBarController from xib tp 0.02 (below 0.01 the tab bar does not catch touch actions). you are done! even when doing all these programmatically... these are very trivial... this round-about journey avoids the apple policy that developers can not subclass UITabBarController. so... cheers :)

Upvotes: 2

Raunak
Raunak

Reputation: 3414

I quickly wrote the following class and showing/hiding tab views from UITabBarController worked like magic:

TabBarDesigner.h

#import <Foundation/Foundation.h>
@interface TabBarDesigner : NSObject 
{

}

+(void) setTabBarController:(UITabBarController *)tabBarController
                      items:(NSArray *)tabBarItems 
            viewControllers:(NSArray *)viewControllers;

+(void) removeItemsInRange:(NSRange) range;

@end

TabBarDesigner.m

#import "TabBarDesigner.h"

static NSArray *_tabBarItems = NULL;
static NSArray *_viewControllers = NULL;
static UITabBarController *_tabBarController = NULL;

@implementation TabBarDesigner

+(void) setTabBarController:(UITabBarController *)tabBarController
                      items:(NSArray *)tabBarItems 
            viewControllers:(NSArray *)viewControllers
{
    if (tabBarItems && viewControllers && tabBarController)
    {
        if ([tabBarItems count] == [viewControllers count])
        {
            [_tabBarItems release];
            [_viewControllers release];
            _tabBarItems = [tabBarItems copy];
            _viewControllers = [viewControllers copy];
            _tabBarController = tabBarController;
        }
    }
}

+(void) removeItemsInRange:(NSRange) range
{
    if (_tabBarController)
    {
        if ( range.location < ([_tabBarItems count] - 1) )
        {
            if ( (range.length + range.location) < [_tabBarItems count] )
            {
                NSMutableArray *tabBarItems = [_tabBarItems mutableCopy];   
                [tabBarItems removeObjectsInRange:range];
                NSMutableArray *viewControllers = [_viewControllers mutableCopy];
                [viewControllers removeObjectsInRange:range];
                [_tabBarController setViewControllers:viewControllers];
                 NSUInteger i;
                for (i = 0; i< [viewControllers count]; i++) 
                {
                    UIViewController *vC = [viewControllers objectAtIndex:i];
                    vC.tabBarItem.image = [[tabBarItems objectAtIndex:i] image];
                    vC.tabBarItem.title = [[tabBarItems objectAtIndex:i] title];
                    vC.tabBarItem.tag = [[tabBarItems objectAtIndex:i] tag];
                }

                [tabBarItems release];
                [viewControllers release];

            }


        }
    }
}


@end

A sample of how to use this class: In your MyAppDelegate.m

#import "TabBarDesigner.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [TabBarDesigner setTabBarController:_tabBarController
                                  items:[_tabBarController.tabBar items] 
                        viewControllers:[_tabBarController viewControllers]];
    // remove the first 3 tabs
    [TabBarDesigner removeItemsInRange:NSMakeRange(0,3)];
    // show all tabs
    [TabBarDesigner removeItemsInRange:NSMakeRange(0,0)];
    // continue with your code
}

Cheers!

Upvotes: 1

Atma
Atma

Reputation: 29767

I figured it out you can get the array of view controllers and then add the images:

           NSArray *tabs =  tabBarController.viewControllers;
UIViewController *tab1 = [tabs objectAtIndex:0];
tab1.tabBarItem.image = [UIImage imageNamed:@"clockicon.png"];
UIViewController *tab2 = [tabs objectAtIndex:1];
tab2.tabBarItem.image = [UIImage imageNamed:@"nearest.png"];

Upvotes: 20

Martin Gordon
Martin Gordon

Reputation: 36389

UIViewController has a tabBarItem property which has an image property (inherited from the UIBarItem class UITabBarItem subclasses). For example:

viewController.tabBarItem.image = [UIImage imageNamed:@"foo.png"];

Upvotes: 12

Related Questions