UITabBarController+UINavigationController 实现自定义TabBar样式

澹台冯浩
2023-12-01

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



 类似资料: