UITabBarConroller使用中遇到的问题总结

这篇博客总结了在使用UITabBarController时遇到的问题,包括如何通过viewController获取标签栏控制器、标签栏及其元素,以及如何自定义tabBar。重点讨论了UITabBarItem的属性及其与UITabBarButton的关系,并提供了在不能直接访问tabBarButton私有属性时的解决思路。同时,给出了UITabBarController添加子控制器的通用方法和自定义tabBar的两种方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UITabBarController:标签栏控制器(通过viewController获取)

  • 1.属性:

    • viewControllers:子控制器数组

    • selectedViewController:选中的控制器

    • selectedIndex:选中的控制器索引

    • tabBar:标签栏

UITabBar:标签栏(通过tabBarController获取)

  • 0.tabBar上的子控件是懒加载的,使用的时候要考虑到这一点,所以在tabBarController中的viewdidLoad中一般是取不到其子控件的

  • 1.属性:

    • items:

    • selectedItem:

    • tintColor:

    • barTintColor:

    • backgroundImage:

    • shadowImage:

    • itemSpacing:

    • barStyle:

    • translucent:

    • delegate:

  • 2.tabBarDelegate:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;


UITabBarItem:标签栏的元素(通过viewController获取)

  • 1.属性:

    • image

    • selectedImage:

    • badgeValue:

    • title

  • 2.初始化方法:

- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image tag:(NSInteger)tag;
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image selectedImage:(nullable UIImage *)selectedImage NS_AVAILABLE_IOS(7_0);
- (instancetype)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag;

UITabBarButton:

  • tabBarButton里面显示什么内容由tabBarItem的属性决定

  • 但是这个是私有属性,正常情况下访问不到,可以考虑用KVO修改其权限.

  • 正常情况下,如果使用系统的tabBar,就直接使用tabBarItem赋值就可以,如果自定义,那就直接使用自定义button好了,不用去管这个.


    UITabBarcontroller代码添加一个子控制器的通用方法:

    - (void)addOneChildViewControll:(UIViewController *)childVC image:(NSString *)imageName selectedImage:(NSString *)selectedImageName title:(NSString *)title{
    
        //每个控制器默认都是自带tabbaritem的,所以我们可以将 childVC.tabBarItem作为一个模型,先给模型赋值数据,最后将模型保存在items这个数组里面
        childVC.tabBarItem.image = [UIImage imageNamed:imageName];
        childVC.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    
        [self.items addObject:childVC.tabBarItem];
    
        //导航控制器的加载
        WZHNavigationController *nav = [[WZHNavigationController alloc]initWithRootViewController:childVC];
        //设置导航条的标题,一定要设置到对应的控制器的navigationItem上,而不是导航控制器的navigationItem上
    //    [nav.navigationItem setTitle:title];
        childVC.navigationItem.title = title;
    
        [self addChildViewController:nav];
    
    }

自定义tabBar:

  • 自定义tabBar,是将自定义的view添加到self.tabBar上面

  • 方案一:

#pragma mark - 加载自定义的底部工具条
- (void)setupBottomView {

    // MARK: - 1.加载并显示自定义的视图
    // 1.创建
    MMBottomView *bottomView = [[MMBottomView alloc] init];

    // 1.2 赋值代码块(该代码块是在tabBar上的按钮点击的时候调用的)
    bottomView.bottomBlock = ^(MMBottomView *bottomView, NSUInteger idx) {
        // 切换索引
        self.selectedIndex = idx;
    };

    // 2.设置frame信息&背景
    bottomView.frame = self.tabBar.bounds;
    bottomView.backgroundColor = MMRandomColor;

    // 3.添加
    // 添加到tabBar身上
    [self.tabBar addSubview:bottomView];

    // MARK: - 2.遍历标签vc的子控制器,每遍历到一个,就让bottomView添加一个按钮
    // 1.遍历子控制器
    [self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UINavigationController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        // 拼接图片名称
        NSString *normalName = [NSString stringWithFormat:@"TabBar%@", @(idx + 1)];
        NSString *selImgName = [NSString stringWithFormat:@"TabBar%@Sel", @(idx + 1)];

        // 让bottomView.添加一个按钮
        [bottomView addButtonWithImage:[UIImage imageNamed:normalName] andSelectImg:[UIImage imageNamed:selImgName]];

    }];
}


  • 方案二:

  /**
 *  设置tabBar
 */
- (void)setupTabBar{

    //首先移除系统的tabBar
 //   [self.tabBar removeFromSuperview];  不能直接移除tabBar,因为自定义的tabBar需要添加到self.tabBar上才能维持tabBar原有哦的属性

    //实例化tabBar需要哪些信息? 图片,被选中图片,这些都存在items里面的模型中

    WZHTabBar *tabBar = [[WZHTabBar alloc]init];

    tabBar.delegate = self;//设置代理   切换控制器是在代理方法中实现的

    tabBar.backgroundColor = [UIColor blackColor];

    //将tabBarItem的模型数组传给tabBar
    tabBar.items = _items;  //这个_items 是在tabBarController加载viewControllres的时候创建的
    //这里用被移除的系统的tabBar的frame来初始化自定义的tabBar,是因为被移除的tabbar不会立即被释放
    tabBar.frame = self.tabBar.bounds;

  //  NSLog(@"tabBar =====%@",tabBar);

    //将tabBar添加到tabBarController上
    [self.tabBar addSubview:tabBar];
}


/*******************代理方法,也是在tabBar上的按钮被点击的时候调用*****************************/
#pragma mark - WZHTabBar的代理方法

- (void)wzhTabBar:(WZHTabBar *)tabBar index:(NSInteger)index{

    self.selectedIndex = index;
}


/*****************************删除系统的tabBar**************************************/

/**
 *  由于tabBar上的子控件是懒加载的 所以在viewDidLoad那里是取不到tabBar的子控件的
    而在这个方法里面,tabBar的子控件已经加载完毕,可以取到,也就可以移除掉了,
    但是这时候,自定义的tabBar也已经加载上去了,所以移除的时候要判断
 *
 */
- (void)viewWillAppear:(BOOL)animated{
    //不能忘记调用父类
    [super viewWillAppear:animated];

    for (UIView * item in self.tabBar.subviews) {

        if (![item isKindOfClass:[WZHTabBar class]]) {
             [item removeFromSuperview];
        }

    }

  //  NSLog(@"%@",self.tabBar.subviews);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值