UITabBarController+UINavigationController 实现自定义TabBar样式

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UITabBarController <UITabBarControllerDelegate, UITabBarDelegate>
{
    NSMutableArray *_tabBarButtons;
}

@property(nonatomic, retain) NSMutableArray *tabBarButtons;

@end

RootViewController.m

#import "RootViewController.h"
#import "MyViewController.h"
#import "UIButton+WebCache.h"

@interface RootViewController ()

@end

@implementation RootViewController

@synthesize tabBarButtons = _tabBarButtons;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        MyViewController *viewController1 = [[MyViewController alloc] init];
        MyViewController *viewController2 = [[MyViewController alloc] init];
        
        UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
        UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];

        NSArray *controllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
        self.viewControllers = controllers;
        
        /* 方法一
        UIImage *image0101 = [UIImage imageNamed:@"0102.png"];
        UIImage *image0102 = [UIImage imageNamed:@"0101.png"];
        UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"精品推荐" image:image0101 selectedImage:image0102];
        navigationController1.tabBarItem = item1;
        
        UIImage *image0201 = [UIImage imageNamed:@"0202.png"];
        UIImage *image0202 = [UIImage imageNamed:@"0201.png"];
        
        UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"限时免费" image:image0201 selectedImage:image0202];
        navigationController2.tabBarItem = item2;
         */
        
        
        int itemCount = [controllers count];
        float itemWidth = 320 / itemCount;
        float imgW = 23;
        float imgH = 22;
        
        _tabBarButtons = [[NSMutableArray alloc] init];
        for (int i=0; i<itemCount; i++){
            UIButton *tabbarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            tabbarBtn.userInteractionEnabled = YES;
            tabbarBtn.frame = CGRectMake(0 + i * itemWidth + (itemWidth - imgW) / 2, 7, imgW, imgH);
            NSString *image1 = [NSString stringWithFormat:@"0%i01.png", i+1];
            NSString *image2 = [NSString stringWithFormat:@"0%i02.png", i+1];
            [tabbarBtn setImage:[UIImage imageNamed: image1] forState:UIControlStateNormal];
            [tabbarBtn setImage:[UIImage imageNamed: image2] forState:UIControlStateSelected];
            [tabbarBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.tabBar addSubview:tabbarBtn];
            
            [_tabBarButtons insertObject:tabbarBtn atIndex:i];
        }
        UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"精品推荐" image:nil selectedImage:nil];
        UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"限时免费" image:nil selectedImage:nil];
        item1.tag = 0;
        item2.tag = 1;
        navigationController1.tabBarItem = item1;
        navigationController2.tabBarItem = item2;
        
        
//        self.selectedIndex = 0;
        [self tabBar:self.tabBar didSelectItem:0];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) btnClick:(id) button
{
    NSLog(@"%@", button);
//    button.selected = YES;
}


#pragma mark - tab bar delegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
     // NSLog(@"%@--%d--%d",item.title, item.tag, self.selectedIndex);
    for (int i=0, count = [_tabBarButtons count]; i<count; i++)
    {
        UIButton *btn = [_tabBarButtons objectAtIndex:i];
        if (i == item.tag){
            btn.selected = YES;
        }else {
            btn.selected = NO;
        }
    }
}

@end



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值