Обновленный ответ
После некоторых комментариев пользователей @switchCTRL, @Ashley Mills для добавления различных способов скрытия и отображения.вы можете сделать подкласс UITabBarController в соответствии с @ switchCTRL
и использовать его, если создаете свой TabbarController
с кодом
CustomTabbarViewController *tabbarController = [[CustomTabbarViewController alloc]init];
tabbarController.viewControllers = [NSArray arrayWithObjects:ViewController1,ViewController2,ViewController3,ViewController4,ViewController5 ,nil];
, если вы создаете в Stroybord, просто убедитесь, что ваш TabbarContoller
'Supercalss is 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];