如何隐藏UITabBar项目?

时间:2018-05-29 07:57:42

标签: ios objective-c xcode

我想创建一个开关按钮来隐藏UITabBar项目。你能解释一下我怎么能创造这个?

我的切换按钮必须在启用时隐藏自动项目,并在关闭时显示。

2 个答案:

答案 0 :(得分:3)

您必须通过UITabBarController从托管控制器阵列中删除您的控制器(而不是直接选项卡),然后再重新添加。

删除项目:

NSMutableArray *viewControllers = (NSMutableArray *) self.tabBarController.viewControllers;
[viewControllers removeObjectAtIndex:2];
[self.tabBarController setViewControllers:viewControllers];

强行引用已移除的控制器并稍后重新添加:

NSMutableArray *viewControllers = (NSMutableArray *) self.tabBarController.viewControllers;
[viewControllers insertObject:yourRemovedController atIndex:2];
[self.tabBarController setViewControllers:viewControllers];

答案 1 :(得分:-1)

更新了答案

经过用户@switchCTRL的一些评论后,@ Ashley Mills添加了不同的隐藏和显示方式。你可以根据@switchCTRL

做UITabBarController的子类

如果您使用代码

创建TabbarController,请使用相同的内容
  CustomTabbarViewController *tabbarController  = [[CustomTabbarViewController alloc]init];
    tabbarController.viewControllers = [NSArray arrayWithObjects:ViewController1,ViewController2,ViewController3,ViewController4,ViewController5 ,nil];

如果您在Stroybord中创建,请确保您的TabbarContoller'Supercalss为CustomTabbarViewController

<强> CustomTabbarViewController:

<强> CustomTabbarViewController.h

#import <UIKit/UIKit.h>

@interface CustomTabbarViewController : UITabBarController

-(void)hideDynamicTabbarItem;
-(void)showDynamicTabbarItem;

@end

<强> CustomTabbarViewController.m

#import "CustomTabbarViewController.h"

@interface CustomTabbarViewController (){
  UIViewController *dynamicController;
}

@end

@implementation CustomTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


-(void)hideDynamicTabbarItem{
    if (self.viewControllers.count > 2) {
        NSMutableArray *allViewControllers = (NSMutableArray*)self.viewControllers;
        dynamicController = allViewControllers[2];
        [allViewControllers removeObjectAtIndex:2];
        self.viewControllers = allViewControllers;
    }
}
-(void)showDynamicTabbarItem{

        NSMutableArray *allViewControllers = (NSMutableArray*)self.viewControllers;
        [allViewControllers insertObject:dynamicController atIndex:2];
        self.viewControllers = allViewControllers;

}


@end

要隐藏:

if ([self.tabBarController isKindOfClass:[CustomTabbarViewController class]]) {

    [(CustomTabbarViewController*)self.tabBarController hideDynamicTabbarItem];
}

显示:

   if ([self.tabBarController isKindOfClass:[CustomTabbarViewController class]]) {

        [(CustomTabbarViewController*)self.tabBarController showDynamicTabbarItem];
    }

另一种简单的解决方案,但不是首选:

AppDeleagte.h文件中添加dynamicController

@interface AppDelegate : UIResponder <UIApplicationDelegate>
   @property (strong, nonatomic) UIViewController *dynamicController;
@end

然后当你隐藏:

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSMutableArray *viewControllers = (NSMutableArray *) 
    self.tabBarController.viewControllers;

    appDelegate.dynamicController = viewControllers[2]; // to add later
    [viewControllers removeObject:APPDELEGATE.dynamicController];
    [self.tabBarController setViewControllers:viewControllers];

当你显示:

  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        NSMutableArray *viewControllers = (NSMutableArray *) self.tabBarController.viewControllers;
       [viewControllers insertObject:appDelegate.dynamicController atIndex:2];
        [self.tabBarController setViewControllers:viewControllers];