这是appdelegate头文件中声明
#import <UIKit/UIKit.h>
@class CustomTabBarViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
CustomTabBarViewController *tabBarCtl;//自定义视图控制器
}
@property (strong, nonatomic) UIWindow *window;
@property (retain, nonatomic) CustomTabBarViewController *tabBarCtl;
@end
在下述方法中初始化控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
tabBarCtl = [[CustomTabBarViewController alloc]init];
//获取各个按钮上图片资源名
NSMutableArray *selectArr = [[NSMutableArray alloc]initWithObjects:@"1_selected.png",@"2_selected.png",@"3_selected.png",@"4_selected.png", nil];
NSMutableArray *normalArr = [[NSMutableArray alloc]initWithObjects:@"1_normal.png",@"2_normal.png",@"3_normal.png",@"4_normal.png", nil];
NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:selectArr,@"select",normalArr,@"normal", nil];
[selectArr release];
[normalArr release];
//构建4个视图控制器,存放与数组当中
UIViewController *v1 = [[UIViewController alloc]init];
v1.view.backgroundColor = [UIColor redColor];
UIViewController *v2 = [[UIViewController alloc]init];
v2.view.backgroundColor = [UIColor yellowColor];
UIViewController *v3 = [[UIViewController alloc]init];
v3.view.backgroundColor = [UIColor greenColor];
UIViewController *v4 = [[UIViewController alloc]init];
v4.view.backgroundColor = [UIColor grayColor];
NSMutableArray *ctlArr = [[NSMutableArray alloc]initWithObjects:v1,v2,v3,v4, nil];
[v1 release];
[v2 release];
[v3 release];
[v4 release];
//初始化tabbar控制器数组和图片字典
tabBarCtl._imgDic = dic;
tabBarCtl._controllerArr = ctlArr;
[tabBarCtl reloadTabBar];//让tabbarCtl重新绘制
[ctlArr release];
[dic release];
self.window.rootViewController = tabBarCtl;
[tabBarCtl release];
self.window.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self.window makeKeyAndVisible];
return YES;
}
主视图控制器头文件声明
#import <UIKit/UIKit.h>
@interface CustomTabBarViewController : UIViewController
{
//所有的视图控制器
NSMutableArray *_controllerArr;
//所有的按钮图片
NSDictionary *_imgDic;
//所有的视图加载界面
UIView *_customView;
//为了隐藏
UIView *_toolBarView;
}
@property (nonatomic,retain) NSMutableArray *_controllerArr;
@property (nonatomic,retain) NSDictionary *_imgDic;
@property (nonatomic,retain) UIView *_toolBarView;
-(void)reloadTabBar;
-(void)hideTabBar;//隐藏选项卡方法
@end
初始化方法
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//初始化选项卡栏大小
_toolBarView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height - 44, 320, 44)];
//初始化内容视图大小
_customView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height - 44)];
_customView.backgroundColor = [UIColor whiteColor];
_customView.autoresizesSubviews = YES;
[self.view addSubview:_toolBarView];
[self.view addSubview:_customView];
}
return self;
}
tabbar上按钮生成及事件处理
-(void)hideTabBar
{
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
delegate.tabBarCtl._toolBarView.hidden = YES;
}
-(void)btnClick:(UIButton *)btn
{
NSMutableArray *selectArr = [_imgDic objectForKey:@"select"];
NSMutableArray *normalArr = [_imgDic objectForKey:@"normal"];
//遍历数组 切换图片 确定按钮显示的图片类型
for (int i = 0; i < [selectArr count]; i++) {
if (btn.tag-ToolBarTag == i) {
[btn setImage:[UIImage imageNamed:[selectArr objectAtIndex:btn.tag - ToolBarTag]] forState:UIControlStateNormal];
} else {
UIButton *button = (UIButton *)[self.view viewWithTag:i+ToolBarTag];
[button setImage:[UIImage imageNamed:[normalArr objectAtIndex:i]] forState:UIControlStateNormal];
}
}
//--------
//加载View之前把上一次_customView的所有子视图移除
for (UIView *v in [_customView subviews]) {
[v removeFromSuperview];
}
//加载当前的按钮所对应的视图控制器的视图
UIViewController *ctl = [_controllerArr objectAtIndex:btn.tag - ToolBarTag];
ctl.view.frame = _customView.frame;
[_customView addSubview:ctl.view];
}
//创建4个按钮加载到toolbar上
-(void)reloadTabBar
{
NSMutableArray *unselectArr = [_imgDic objectForKey:@"normal"];
for (int i = 0; i < [unselectArr count]; i++) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
b.adjustsImageWhenDisabled = NO;
b.adjustsImageWhenHighlighted = NO;
UIImage *img = [UIImage imageNamed:@"section_bg.png"];
[img stretchableImageWithLeftCapWidth:0 topCapHeight:0];
[b setBackgroundImage:img forState:UIControlStateNormal];
[b setImage:[UIImage imageNamed:[unselectArr objectAtIndex:i]] forState:UIControlStateNormal];
b.frame = CGRectMake(80*i, 0, 80, 44);
b.tag = ToolBarTag+i;
[b addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[_toolBarView addSubview:b];
}
}