iOS 自定义TabBarController

这是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];
    }
}














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值