How to set image for uitabbarcontroller in cocoa code

Hey. I am creating a tab bar controller in xcode, not in the interface builder. tab view headings set tab headings, but I'm not sure how to set images.

Can anyone help?

+5
source share
4 answers

I realized that you can get an array of view controllers and then add 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"];
+19
source

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

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

UITabBarController , , , . . UITabBarController UITabBarController ( ) xib, UIView UITabBarController ( "view" ). , , , . , . UITabBarController xib tp 0,02 ( 0,01 ). ! ... ... Apple, UITabBarController. ... :)

+2

/ UITabBarController, 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

: 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
}

!

+1

All Articles