开发者

How to set the default image to tabbar item programmatically?

开发者 https://www.devze.com 2023-01-29 09:10 出处:网络
I am creating a tabbar with 5开发者_开发百科 tab items. I have created the tabbar programmatically. I want to set the default image(More) to tabbar item. If I create tabbar through IB I would select t

I am creating a tabbar with 5开发者_开发百科 tab items. I have created the tabbar programmatically. I want to set the default image(More) to tabbar item. If I create tabbar through IB I would select the identifier as "More", but how to do it programmatically?


For this purpose you need to create a class whose parent class will be UITabBar.

Here is its .h file:

    #import <UIKit/UIKit.h>


    @interface ImageTabBar : UITabBar 
    {

    }

    @end

And here is its .m file:

    #import "ImageTabBar.h"
    #import "GlobalVars.h"

    @implementation ImageTabBar

    - (void) drawRect:(CGRect)rect
    {
        UIImage *tabImage;
        switch (intTabBarSelectedIndex) 
        {
            case 0:
                tabImage=[UIImage imageNamed:@"TabBarImageTwitter.png"];
                [tabImage drawAtPoint:CGPointMake(0, 0)];
                break;
            case 1:
                tabImage=[UIImage imageNamed:@"TabBarImageCalender.png"];
                [tabImage drawAtPoint:CGPointMake(0, 0)];
                break;

            case 2:
                tabImage=[UIImage imageNamed:@"TabBarImageStanding.png"];
                [tabImage drawAtPoint:CGPointMake(0, 0)];
                break;

            case 3:
                tabImage=[UIImage imageNamed:@"TabBarImageNews.png"];
                [tabImage drawAtPoint:CGPointMake(0, 0)];
                break;

            case 4:
                tabImage=[UIImage imageNamed:@"TabBarImagePhotos.png"];
                [tabImage drawAtPoint:CGPointMake(0, 0)];
                break;

            default:
                break;
        }
    }

    - (void)dealloc 
    {
        [super dealloc];
    }


    @end

In the statement:

switch (intTabBarSelectedIndex) 

intTabBarSelectedIndex will be defined as global variable and is of integer type.

Now come to TabBarController class.

Here is its .h file

#import <UIKit/UIKit.h>

@class  ImageTabBar;

@interface TabBarViewController : UIViewController <UITabBarControllerDelegate>
{
    ImageTabBar *objOfImageTabBar;

    IBOutlet UITabBarController *uiTabBarC;
}

@property (nonatomic, retain) IBOutlet UITabBarController   *uiTabBarC;

- (void)setNeedsDisplay;

@end

And here is its .m file

Include the following functions in your TabBarController class' .m file:

- (void)viewDidLoad 
{
    [self.navigationController setNavigationBarHidden:YES];

    self.view               = uiTabBarC.view;
    uiTabBarC.selectedIndex = intTabBarSelectedIndex;
    uiTabBarC.delegate      = self;
    [super viewDidLoad];
}

#pragma mark TaBarViewController delegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    intTabBarSelectedIndex = uiTabBarC.selectedIndex;

    UITabBar *aTabBar = tabBarController.tabBar;
    [aTabBar setNeedsDisplay];
}

- (void)setNeedsDisplay
{

}

Do exactly like this, your code will run perfectly.

0

精彩评论

暂无评论...
验证码 换一张
取 消