iOS 改变UITabBarController的控制器数量

代码如下:

#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设置->辅助功能->在底部栏展示电话功能

Demo下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标...
    VincentHK阅读 5,470评论 3 44
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,590评论 1 14
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,617评论 2 4
  • 我是一个摊煎饼的。 干我们这行,唉,就别提了。 天天练摊风吹日晒雨淋就不说了,被城管追打也就不说了,关键是连摊个煎...
    刀爷阅读 1,126评论 14 8
  • 作为一个孕妇是很辛苦的!作为一个会孕吐的孕妇比阵痛来的更辛苦! 如果你不会孕吐,恭喜你!你要像国足踢进世界杯一样为...
    笑茶锈茶阅读 159评论 0 0