自定义的tabBarController的几种方法

本文转载自:http://blog.sina.com.cn/s/blog_79c5bdc30100t88i.html

我自己实现的一种可以很方便的实现更换TabBarController图片的方法,代码如下:

#import 

@interface CustomTabBarViewController : UITabBarController {

    UIImage                             *_tabBarBackgroundImage;  //整个tabBar的背景

    NSMutableArray                       *_unSelectedImageArray;   //非选中效果的tabBarItem数组

    NSMutableArray                       *_selectedImageArray;     //选中效果的tabBarItem数组

    NSMutableArray                       *_itemBgImageViewArray;     //item背景UIIMageView数组   

    int                                  _lastSelectedIndex;   //上一次选中的tabBarItemindex

    int                                  _hiddenIndex;

}

@property (nonatomicretainUIImage                           *tabBarBackgroundImage;

@property (nonatomicretainNSMutableArray                    *unSelectedImageArray;

@property (nonatomicretainNSMutableArray                    *selectedImageArray;

@property (nonatomicretainNSMutableArray                    *itemBgImageViewArray;

@property (nonatomicassignint                               lastSelectedIndex;

@property (nonatomicassignint                               hiddenIndex;


- (id)initWithTabBarBackgroundImage:(UIImage *)barBackgroundImage 

               unSelectedImageArray:(NSMutableArray *)unImageArray

                 selectedImageArray:(NSMutableArray *)imageArray;


//隐藏某个tabBarItem的图片

-(void)hiddeItemImageView:(int)index;

//显示某个tabBarItem的图片

-(void)showItemImageView:(int)index;

@end


 


#import "CustomTabBarViewController.h"



#define TabBarBackgroundImageViewTag 11111


@implementation CustomTabBarViewController


@synthesize tabBarBackgroundImage                       = _tabBarBackgroundImage;

@synthesize unSelectedImageArray                        = _unSelectedImageArray;

@synthesize selectedImageArray                          = _selectedImageArray;

@synthesize itemBgImageViewArray                        = _itemBgImageViewArray;

@synthesize lastSelectedIndex                           = _lastSelectedIndex;

@synthesize hiddenIndex                                 = _hiddenIndex;


- (void)dealloc

{   

    self.tabBarBackgroundImage = nil;

    self.unSelectedImageArray = nil;

    self.selectedImageArray = nil;

    self.itemBgImageViewArray = nil;

    [super dealloc];

}



- (void)didReceiveMemoryWarning

{

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

    // Release any cached data, images, etc that aren't in use.

    self.tabBarBackgroundImage = nil;

    

}


- (id)initWithTabBarBackgroundImage:(UIImage *)barBackgroundImage 

               unSelectedImageArray:(NSMutableArray *)unImageArray

                 selectedImageArray:(NSMutableArray *)imageArray {

    self = [super init];

    if (self) {

        

//        self.tabBarBackgroundImage = barBackgroundImage;

//        self.unSelectedImageArray = unImageArray;

//        self.selectedImageArray = imageArray;

//        

//        self.itemBgImageViewArray = [NSMutableArray array];

//        _lastSelectedIndex = 0;

//        _hiddenIndex = -1;

    }

    return self;

}


- (id)init {

    self = [super init];

    if (self) {

//        self.tabBarBackgroundImage = [UIImage imageNamed:@"menu_bg.png"];

//        

//        NSMutableArray *aunSelectedImageArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"menu_paiyou.png"], 

//                                                 [UIImage imageNamed:@"menu_chat.png"], 

//                                                 [UIImage imageNamed:@"menu_map.png"], 

//                                                 [UIImage imageNamed:@"menu_renwu.png"],

//                                                 [UIImage imageNamed:@"menu_huibao.png"], nil];

//        self.unSelectedImageArray = aunSelectedImageArray;

//        [aunSelectedImageArray release];

//        

//        NSMutableArray *aselectedImageArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"menu_paiyou_pressed.png"], 

//                                               [UIImage imageNamed:@"menu_chat_pressed.png"], 

//                                               [UIImage imageNamed:@"menu_map_pressed.png"], 

//                                               [UIImage imageNamed:@"menu_renwu_pressed.png"], 

//                                               [UIImage imageNamed:@"menu_huibao_pressed.png"], nil];

//        self.selectedImageArray = aselectedImageArray;

//        [aselectedImageArray release];

//        

//        self.itemBgImageViewArray = [NSMutableArray array];

//        _lastSelectedIndex = 0;

//        _hiddenIndex = -1;

        

    }

    return self;

}


#pragma mark - itemIndex methods


- (void)setLastSelectedIndex:(int)lastSelectedIndex {

    if (_lastSelectedIndex != lastSelectedIndex) {

        //将上次的选中效果取消

        UIImageView *lastSelectedImageView = (UIImageView *)[_itemBgImageViewArray objectAtIndex:_lastSelectedIndex];;

        lastSelectedImageView.image = [_unSelectedImageArray objectAtIndex:_lastSelectedIndex];

        

        _lastSelectedIndex = lastSelectedIndex;

    }

}


- (void)setSelectedIndex:(NSUInteger)selectedIndex {

    [super setSelectedIndex:selectedIndex];

    //将上次的选中效果取消

    self.lastSelectedIndex = selectedIndex;

    //将本次的选中效果显示

    UIImageView *selectedImageView = (UIImageView *)[_itemBgImageViewArray objectAtIndex:selectedIndex];

    selectedImageView.image = [_selectedImageArray objectAtIndex:selectedIndex];

             

}


//隐藏某个tabBarItem的图片

- (void)hiddeItemImageView:(int)index {

    if (_hiddenIndex != index) {

        _hiddenIndex = index;

        

        UIImageView *hiddenImageView = (UIImageView *)[_itemBgImageViewArray objectAtIndex:_hiddenIndex];

        hiddenImageView.hidden = YES;

    }

}


//显示某个tabBarItem的图片

- (void)showItemImageView:(int)index {

    if (_hiddenIndex == index) {

        

        UIImageView *hiddenImageView = (UIImageView *)[_itemBgImageViewArray objectAtIndex:_hiddenIndex];

        hiddenImageView.hidden = NO;

        

        _hiddenIndex = -1;

    }

}


#pragma mark - View lifecycle


// Implement loadView to create a view hierarchy programmatically, without using a nib.

- (void)loadView

{

    [super loadView];

        

    self.tabBarBackgroundImage = [UIImage imageNamed:@"menu_bg.png"];

    

    NSMutableArray *aunSelectedImageArray = [[NSMutableArray allocinitWithObjects:[UIImageimageNamed:@"menu_paiyou.png"], 

                                             [UIImage imageNamed:@"menu_chat.png"], 

                                             [UIImage imageNamed:@"menu_map.png"], 

                                             [UIImage imageNamed:@"menu_renwu.png"],

                                             [UIImage imageNamed:@"menu_huibao.png"], nil];

    self.unSelectedImageArray = aunSelectedImageArray;

    [aunSelectedImageArray release];

    

    NSMutableArray *aselectedImageArray = [[NSMutableArray allocinitWithObjects:[UIImageimageNamed:@"menu_paiyou_pressed.png"], 

                                           [UIImage imageNamed:@"menu_chat_pressed.png"], 

                                           [UIImage imageNamed:@"menu_map_pressed.png"], 

                                           [UIImage imageNamed:@"menu_renwu_pressed.png"], 

                                           [UIImage imageNamed:@"menu_huibao_pressed.png"], nil];

    self.selectedImageArray = aselectedImageArray;

    [aselectedImageArray release];

    

    self.itemBgImageViewArray = [NSMutableArray array];

    _lastSelectedIndex = 0;

    _hiddenIndex = -1;

}

 


#define ItemWidth 50

#define ItemHeight 49

#define SideMarginX 7

#define SideMarginY 0.5

#define Spacing 14


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad

{

    [super viewDidLoad];

    UIImageView *tabBarBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,self.tabBar.frame.size.width, self.tabBar.frame.size.height)];

    tabBarBackgroundImageView.tag = TabBarBackgroundImageViewTag;

    tabBarBackgroundImageView.contentMode = UIViewContentModeScaleAspectFit;

    tabBarBackgroundImageView.image = _tabBarBackgroundImage;

    [self.tabBar insertSubview:tabBarBackgroundImageView atIndex:0];

    [tabBarBackgroundImageView release];

 for (int i = 0; i < 5; i++) {

UIImageView *itemBg  = [[UIImageView alloc] initWithFrame:CGRectMake(SideMarginX +ItemWidth * i + Spacing * i, SideMarginY, ItemWidth, ItemHeight)];

    itemBg.contentMode = UIViewContentModeScaleAspectFit;

    itemBg.image = [_unSelectedImageArray objectAtIndex:i];

    [self.tabBar insertSubview:itemBg atIndex:1];

    [_itemBgImageViewArray addObject:itemBg];

    [itemBg release];

}

    self.selectedIndex = 0;

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

    self.itemBgImageViewArray = nil;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


#pragma mark - UITabBarDelegate

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

    self.selectedIndex = [tabBar.items indexOfObject:item];

}


@end



方法二:http://www.cocoachina.com/bbs/read.php?tid-62061-page-1.html

方法三:http://hi.baidu.com/yunhuaikong/blog/item/b0642bcbfc5ae49fc817684c.html
自定义Badge:https://github.com/boctor/idev-recipes/tree/master/CustomTabBarNotification
一些自定义控件:https://github.com/boctor/idev-recipes/tree/master/CustomTabBarNotification

你可能感兴趣的:(ios技术)