方法一 隐藏式
原理:将其tabbar即将要出现的时候,将其隐藏,自己写一个UIView类,上面放UIButton类,方法比较麻烦,适合于<IOS 5的,从IOS 6开始后,有了一个新的方法,用于设置其背景图片和选中的图片
代码
.h头文件
#import <UIKit/UIKit.h>
@interface CustomTabBarController : UITabBarController
{
//用于保存上次处于选中的tabbaritem
UIImageView * imagView;
UILabel * lab;
}
@property (nonatomic,retain) NSMutableArray * tabBarImageNames;
@property (nonatomic,retain) NSMutableArray * tabBarSelectedImageNames;
@property (nonatomic,retain) NSMutableArray * tabBarTitleNames;
@property (nonatomic,retain) NSMutableArray * bnts;
@property (nonatomic,retain) UIView * tabBarBackgroundView;
@end
.m文件
#import "CustomTabBarController.h"
#define btnWidth kScreenWidth/self.viewControllers.count
#define btnHeight self.tabBar.frame.size.height
@interface CustomTabBarController ()
@end
@implementation CustomTabBarController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//当程序刚启动时隐藏tabBar
[self.tabBar setHidden:YES];
}
return self;
}
//初始化自定义的tabBar
- (void)viewWillAppear:(BOOL)animated
{
self.tabBarImageNames = [[NSMutableArray alloc]initWithObjects:
@"图标1.png",
@"图标2.png",
@"图标3.png",
@"图标4.png",
nil];
self.tabBarSelectedImageNames = [[NSMutableArray alloc]initWithObjects:
@"图标11.png",
@"图标12.png",
@"图标13.png",
@"图标14.png",
nil];
self.tabBarTitleNames = [[NSMutableArray alloc]initWithObjects:
@"我的收藏",
@"当前位置",
@"自选位置",
@"更多",
nil];
//初始化自定义的tabbar
[self initCustomTabBar];
}
-(void)initCustomTabBar
{
//初始化数据
self.bnts = [[NSMutableArray alloc]initWithCapacity:self.viewControllers.count];
//tabBar背景视图
UIColor * tabBarBackgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"标题栏背景"]];
self.tabBarBackgroundView = [[UIView alloc]initWithFrame:self.tabBar.frame];//1
self.tabBarBackgroundView.backgroundColor = tabBarBackgroundColor;
[self.view insertSubview:self.tabBarBackgroundView atIndex:1];
//添加btns
int btnCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
for (int i = 0 ; i < btnCount; i++) {
//imageView
NSString * imageName = [self.tabBarImageNames objectAtIndex:i];
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,btnWidth, btnHeight)];
imageView.tag = 1;
imageView.image = [UIImage imageNamed:imageName];
//label
NSString * imageTitle = [self.tabBarTitleNames objectAtIndex:i];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0,btnHeight-22, btnWidth, btnHeight-22)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:77/255.0
green:36/255.0
blue:21/255.0
alpha:1.0];
[label setFont:[UIFont systemFontOfSize:12.0]];
[label setTextAlignment:UITextAlignmentCenter];
[label setText:imageTitle];
UIButton * btnTemp = [UIButton buttonWithType:UIButtonTypeCustom];
btnTemp.frame = CGRectMake(i*btnWidth, 0, btnWidth, btnHeight);
btnTemp.tag = i;
[btnTemp addTarget:self action:@selector(selectedTabbarItem:) forControlEvents:UIControlEventTouchUpInside];
[btnTemp addSubview:imageView];
[btnTemp addSubview:label];
[imageView release];
[label release];
[self.tabBarBackgroundView addSubview:btnTemp];
[self.bnts addObject:btnTemp];
}
[self selectedTabbarItem:[self.bnts objectAtIndex:self.selectedIndex]];
}
- (void)selectedTabbarItem:(UIButton *)button
{
//转到相应的视图
self.selectedIndex = button.tag;
//关键步骤 如果imagView和lab有值,将其移除
if ([imagView superview]) {
[imagView removeFromSuperview];
}
if ([lab superview]) {
[lab removeFromSuperview];
}
//imageView
NSString * imageName = [self.tabBarSelectedImageNames objectAtIndex:button.tag];
imagView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,btnWidth, btnHeight)];
imagView.image = [UIImage imageNamed:imageName];
//label
NSString * imageTitle = [self.tabBarTitleNames objectAtIndex:button.tag];
lab = [[UILabel alloc]initWithFrame:CGRectMake(0,btnHeight-22, btnWidth, btnHeight-22)];
lab.backgroundColor = [UIColor clearColor];
lab.textColor = [UIColor whiteColor];
[lab setFont:[UIFont systemFontOfSize:12.0]];
[lab setTextAlignment:UITextAlignmentCenter];
[lab setText:imageTitle];
[imagView addSubview:lab];
[button addSubview:imagView];
}