UITabBarController相关

代码创建tabBar,添加NavigationController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    UITabBarController *tabBarController = [[UITabBarController alloc]init];

    UIViewController *viewController1 = [[UIViewController alloc]init];
    viewController1.view.backgroundColor = [UIColor redColor];
    [viewController1.navigationItem setTitle:@"viewController1"];
    UINavigationController *navController1 = [[UINavigationController alloc]initWithRootViewController:viewController1];
    [navController1 setTitle:@"导航1"];

    [tabBarController addChildViewController:navController1];

    UIViewController *viewController2 = [[UIViewController alloc]init];
    viewController2.view.backgroundColor = [UIColor greenColor];
    [viewController2.navigationItem setTitle:@"viewController2"];
    UINavigationController *navController2 = [[UINavigationController alloc]initWithRootViewController:viewController2];
    [navController2 setTitle:@"导航2"];
    [tabBarController addChildViewController:navController2];

    UIViewController *viewController3 = [[UIViewController alloc]init];
    viewController3.view.backgroundColor = [UIColor blueColor];
    [viewController3.navigationItem setTitle:@"viewController3"];
    UINavigationController *navController3 = [[UINavigationController alloc]initWithRootViewController:viewController3];
    [navController3 setTitle:@"导航3"];
    [tabBarController addChildViewController:navController3];

    self.window.rootViewController = tabBarController;

    [self.window makeKeyAndVisible];

    return YES;
}

导航栏设置图标、文字、提醒

需要从导航栏每个标签对应的控制器中设置

[navController1.tabBarItem setTitle:@"导航1"];
[navController1.tabBarItem setBadgeValue:@"10"];
[navController1.tabBarItem setImage:(UIImage * _Nullable)];
[navController1.tabBarItem setSelectedImage:(UIImage * _Nullable)];

一个自定义TabBarController的实例

在原有的UITabBarController中删除原有的tabBar,改用自定义的TabBar

ZFMainTabBarController.m

#import "ZFMainTabBarController.h"
#import "ZFPurchaseViewController.h"
#import "ZFArenaViewController.h"
#import "ZFInformationViewController.h"
#import "ZFDiscoveryViewController.h"
#import "ZFMyLotteryViewController.h"
#import "ZFCustomTabBar.h"

@interface ZFMainTabBarController () <ZFCustomTabBarDelegate>

@property(strong,nonatomic)NSMutableArray<UITabBarItem*> *items;

@property(strong,nonatomic)ZFPurchaseViewController *purchaseViewController;
@property(strong,nonatomic)ZFArenaViewController *arenaViewController;
@property(strong,nonatomic)ZFDiscoveryViewController *discoveryViewController;
@property(strong,nonatomic)ZFInformationViewController *informationViewController;
@property(strong,nonatomic)ZFMyLotteryViewController *myLotteryViewController;

@end

@implementation ZFMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configTabbar];
}

- (NSMutableArray<UITabBarItem*> *)items
{
    if(_items == nil)
    {
        _items = [NSMutableArray array];
    }
    return _items;
}

//配置tabBar的每个分控制器,将控制器的数据模型tabBarItem收集起来传入自定义的tabBar中
- (void)configTabbar
{
    self.purchaseViewController = [[ZFPurchaseViewController alloc] init];

    UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:self.purchaseViewController];
    [navController1 setTitle:@"购彩大厅"];
    [self addChildViewController:navController1];
    navController1.tabBarItem.image = [UIImage imageNamed:@"TabBar_Hall_new"];
    navController1.tabBarItem.selectedImage = [UIImage imageNamed:@"TabBar_Hall_selected_new"];
    [self.items addObject:navController1.tabBarItem];

    self.arenaViewController = [[ZFArenaViewController alloc] init];

    UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:self.arenaViewController];
    [navController2 setTitle:@"竞技场"];
    [self addChildViewController:navController2];
    navController2.tabBarItem.image = [UIImage imageNamed:@"TabBar_Arena_new"];
    navController2.tabBarItem.selectedImage = [UIImage imageNamed:@"TabBar_Arena_selected_new"];
    [self.items addObject:navController2.tabBarItem];

    self.discoveryViewController = [[ZFDiscoveryViewController alloc] init];

    UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:self.discoveryViewController];
    [navController3 setTitle:@"发现"];
    [self addChildViewController:navController3];
    navController3.tabBarItem.image = [UIImage imageNamed:@"TabBar_Discovery_new"];
    navController3.tabBarItem.selectedImage = [UIImage imageNamed:@"TabBar_Discovery_selected_new"];
    [self.items addObject:navController3.tabBarItem];

    self.informationViewController = [[ZFInformationViewController alloc] init];

    UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:self.informationViewController];
    [navController4 setTitle:@"开奖信息"];
    [self addChildViewController:navController4];
    navController4.tabBarItem.image = [UIImage imageNamed:@"TabBar_History_new"];
    navController4.tabBarItem.selectedImage = [UIImage imageNamed:@"TabBar_History_selected_new"];
    [self.items addObject:navController4.tabBarItem];

    self.myLotteryViewController = [[ZFMyLotteryViewController alloc] init];

    UINavigationController *navController5 = [[UINavigationController alloc] initWithRootViewController:self.myLotteryViewController];
    [navController5 setTitle:@"我的彩票"];
    [self addChildViewController:navController5];
    navController5.tabBarItem.image = [UIImage imageNamed:@"TabBar_MyLottery_new"];
    navController5.tabBarItem.selectedImage = [UIImage imageNamed:@"TabBar_MyLottery_selected_new"];
    [self.items addObject:navController5.tabBarItem];

    [self.tabBar removeFromSuperview];

    ZFCustomTabBar *customTabBar = [[ZFCustomTabBar alloc]init];
    customTabBar.delegate = self;
    customTabBar.frame = self.tabBar.frame;
    customTabBar.items = self.items;
    [self.view addSubview:customTabBar];
}

//实现自定义的代理方法,拿到当前选择的控制器索引
- (void)customTabBar:(ZFCustomTabBar*) customTabBar indexOfSelected:(NSInteger)index
{
    self.selectedIndex = index;
}

@end

ZFCustomTabBar.h

#import <UIKit/UIKit.h>
#import "ZFCustomTabBarDelegate.h"

@interface ZFCustomTabBar : UIView

@property (weak, nonatomic)id<ZFCustomTabBarDelegate> delegate;
@property (strong, nonatomic)NSMutableArray<UITabBarItem*> *items;
@property (assign, nonatomic)NSInteger selectedIndex;

@end

ZFCustomTabBar.m

#import "ZFCustomTabBar.h"

@interface ZFCustomTabBar()

@property(strong,nonatomic)NSMutableArray<UIButton*>* buttonArray;

@end

@implementation ZFCustomTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor colorWithRed:13.0f/255 green:13.0f/255 blue:13.0f/255 alpha:1];
    }
    return self;
}

- (void)buttonSelected:(UIButton*)button
{
    self.selectedIndex = [self.buttonArray indexOfObject:button];

    for(int i = 0; i < self.buttonArray.count; i++)
    {
        if(i == self.selectedIndex)
        {
            self.buttonArray[i].selected = YES;
        }
        else
        {
            self.buttonArray[i].selected = NO;
        }
    }

    [self.delegate customTabBar:self indexOfSelected:self.selectedIndex];
}

- (void)setItems:(NSMutableArray<UITabBarItem*>*)items
{
    self.buttonArray = [NSMutableArray array];
    for(int i = 0; i < items.count; i++)
    {
        UIButton *button = [[UIButton alloc] init];
        [button addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:button];
        [self addSubview:button];

        if(i == 0)
        {
            [self buttonSelected:button];
        }

        [self.buttonArray[i] setImage:items[i].image forState:UIControlStateNormal];
        [self.buttonArray[i] setImage:items[i].selectedImage forState:UIControlStateSelected];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    for(int i = 0; i < self.buttonArray.count; i++)
    {
        CGFloat buttonW = self.frame.size.width / self.buttonArray.count;
        CGFloat buttonH = self.frame.size.height;
        self.buttonArray[i].frame = CGRectMake(i * buttonW, 0, buttonW, buttonH);
    }
}

@end

ZFCustomTabBarDelegate.h

#import <Foundation/Foundation.h>

@class ZFCustomTabBar;

@protocol ZFCustomTabBarDelegate <NSObject>

- (void)customTabBar:(ZFCustomTabBar*) customTabBar indexOfSelected:(NSInteger)index;

@end

results matching ""

    No results matching ""