自定义Tabbar方法二

方法一    隐藏式

原理:将其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;

    //关键步骤 如果imagViewlab有值,将其移除

    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];

    

    

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值