代码如下:
#import "ZLMTabBarConteroller.h"
@interface ZLMTabBarConteroller ()
/** 控制器数组 */
@property (nonatomic, strong) NSArray *ControllerMutableArry;
/** 标题数组*/
@property (nonatomic, strong) NSArray *TitleMutableArry;
/** item选中图标数组 */
@property (nonatomic, strong) NSArray *SelectImagerMutableArry;
/** item未选中图标数组 */
@property (nonatomic, strong) NSArray *NomalImageMutableArry;
/** 标签控制器子控制器数组 */
@property (nonatomic, strong) NSMutableArray *childControllerArray;
@end
@implementation ZLMTabBarConteroller
- (void)viewDidLoad {
[super viewDidLoad];
// 添加子控制器
[self addchildController];
// 注册监听者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addMorechildController) name:@"key" object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
if ([self.view window] == nil) {
self.view = nil;
}
}
// 移除监听者
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"key" object:nil];
}
// 添加标签子控制器
- (void)addchildController {
for (int i = 0; i < self.ControllerMutableArry.count; i ++) {
Class class = NSClassFromString(self.ControllerMutableArry[i]);
UIViewController *VC = [[class alloc] init];
UINavigationController *NaVC = [[UINavigationController alloc] initWithRootViewController:VC];
NaVC.tabBarItem.title = self.TitleMutableArry[i];
NaVC.tabBarItem.selectedImage = [UIImage imageNamed:self.SelectImagerMutableArry[i]];
NaVC.tabBarItem.image = [UIImage imageNamed:self.NomalImageMutableArry[i]];
// 存放子控制器
[self.childControllerArray addObject:NaVC];
}
self.viewControllers = self.childControllerArray;
self.tabBar.tintColor = [UIColor redColor];
}
// 在指定位置添加或删除一个标签子控制器
- (void)addMorechildController {
if (4 == self.childControllerArray.count) {
Class class = NSClassFromString(@"ZLMMineViewController");
UIViewController *VC = [[class alloc] init];
UINavigationController *NaVC = [[UINavigationController alloc] initWithRootViewController:VC];
NaVC.tabBarItem.title = @"我的";
NaVC.tabBarItem.selectedImage = [UIImage imageNamed:@"tab5_p"];
NaVC.tabBarItem.image = [UIImage imageNamed:@"tab5"];
[self.childControllerArray insertObject:NaVC atIndex:2];
}else {
[self.childControllerArray removeObjectAtIndex:2];
}
self.viewControllers = self.childControllerArray;
}
#pragma mark- 懒加载
- (NSArray *)ControllerMutableArry {
if (!_ControllerMutableArry) {
_ControllerMutableArry = @[@"ZLMHomeViewController",@"ZLMCategoryController",@"ZLMSaleViewController",@"ZLMShoppingCartsViewController"];
}
return _ControllerMutableArry;
}
- (NSArray *)TitleMutableArry {
if (!_TitleMutableArry) {
_TitleMutableArry = @[@"首页",@"分类",@"促销",@"购物车"];
}
return _TitleMutableArry;
}
- (NSArray *)SelectImagerMutableArry {
if (!_SelectImagerMutableArry) {
_SelectImagerMutableArry = @[@"tab1_p",@"tab2_p",@"tab3_p",@"tab4_p"];
}
return _SelectImagerMutableArry;
}
- (NSArray *)NomalImageMutableArry {
if (!_NomalImageMutableArry) {
_NomalImageMutableArry = @[@"tab1",@"tab2",@"tab3",@"tab4"];
}
return _NomalImageMutableArry;
}
- (NSMutableArray *)childControllerArray {
if (!_childControllerArray) {
_childControllerArray = [[NSMutableArray alloc] init];
}
return _childControllerArray;
}
重点:
- 初始化好的子控制器放在childControllerArray可变数组中
- 添加一个通知,当某个事件触发该通知时,通知执行删除或添加子控制器的方法,在childControllerArray的指定位置添加或删除某个子控制器
- 将操作完成后的childControllerArray重新赋值给UITabBarController,self.viewControllers = self.childControllerArray; ,这样就完成了改变UITabBarController中子控制器数量的操作
功能参考:
QQ设置->辅助功能->在底部栏展示电话功能