我在2个独立的viewcontrollers中有2个集合视图,它们位于tabbar中。我正在尝试隐藏tabbar项目2,直到加载tabbar 1。我们有什么方法可以隐藏某个标签栏项吗?最初在viewWillAppear中我想隐藏tabBar Item ..这是我的代码:
隐藏标签栏2
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//HIDE TABBAR2
}
显示标签栏2
- (void)loadCollectionView
{
[self.collectionView reloadData];
[self.collectionView performBatchUpdates:^{}
completion:^(BOOL finished) {
//Unhide TabBar
}];
}
答案 0 :(得分:1)
对UITabBarController
进行子类化(称之为MainTabController
)。在界面构建器中,将选项卡栏控制器对象的类类型设置为MainTabController
类型。
在MainTabController
中创建一个强大的属性来存储第二个视图控制器(将隐藏的那个):
@property (nonatomic, strong) UIViewController *secondTabVC;
然后在viewDidLoad
:
// Store the second tab's VC for later
self.secondTabVC = self.viewControllers[1];
// Hide the second tab's VC
self.viewControllers = @[self.viewControllers[0]];
加载数据后:
// Show the second tab's VC
self.viewControllers = @[self.viewControllers[0], self.secondTabVC];
答案 1 :(得分:1)
您需要从tabbar
删除该视图控制器,您无法隐藏特定的标签栏项目!所以你可以管理它,
NSMutableArray *viewControllersArr = [self.tabBarController.viewControllers mutableCopy];
[viewControllersArr removeObjectAtIndex:1];
[self.tabBarController setViewControllers:viewControllersArr animated:YES];
当您收到集合视图数据或想要显示第二个标签时,您可以这样做
NSMutableArray *viewControllersArr = [self.tabBarController.viewControllers mutableCopy];
UIViewController *yourSeconVc = [self.storyboard instantiateViewControllerWithIdentifier:@"yourStoryboardIdentifier"]; // or whatever way your view controller is for second tab
[viewControllersArr addObject:yourSeconVc];
[self.tabBarController setViewControllers:viewControllersArr animated:YES];
答案 2 :(得分:1)
试试此代码
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[_tabController setViewControllers:@[viewcontroller1,viewcontroller3]];
}
在此处显示标签2
- (void)loadCollectionView
{
[self.collectionView reloadData];
[self.collectionView performBatchUpdates:^{}
completion:^(BOOL finished)
{
[_tabController setViewControllers:@[viewcontroller1,viewcontroller2,viewcontroller3]];
}];
}