最近遇到個需求
游客模式下 先加載好UITabBarcontroller
然后登錄之后 要根據 服務器返回的一個字段來 判斷 改變TabBarcontroller里面的控制器
比方說 i == 0 tabbarcontroller 結構就不變
i ==1 tabbarcontroller 第三個TabBar 就移除當前的控制器
就換成另外的控制器
我總結了2種解決方案 (個人感覺只能將就用用)如果有更好的實現方式 歡迎@我
第一種方式 更換UINavigationController里面的UIViewController
//得到第3個需要變的navi控制器
HBBaseNavigationController *navi = [self.viewControllers objectAtIndex:2];
HBMyAssetsSlideViewController *assets = [[HBMyAssetsSlideViewController alloc] init];
[navi setViewControllers:@[assets] animated:NO];
得到的結果這樣
CF16EB5B-8266-4F92-9A1C-452BB286492A.png
];
修改代碼如下
//得到第3個需要變的navi控制器
HBBaseNavigationController *navi = [self.viewControllers objectAtIndex:2];
HBMyAssetsSlideViewController *assets = [[HBMyAssetsSlideViewController alloc] init];
//重新設置更換的視圖控制器的屬性(感覺多余 可是不寫被替換的就空白)
[self addChildVcItem:assets title:@"資產" image:@"TabBar5" selectedImage:@"TabBar5Sel"];
[navi setViewControllers:@[assets] animated:NO];
//重新設置了一下他的標題和TabBar內容
- (void)addChildVcItem:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
// 設置子控制器的文字
childVc.title = title; // 同時設置tabbar和navigationBar的文字
childVc.tabBarItem.title = title;
// 設置子控制器的圖片
childVc.tabBarItem.image = [UIImage imageNamed:image];
//聲明顯示圖片的原始式樣 不要渲染
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
第二種方式 重新布局viewcontrollers
HBMyAssetsSlideViewController *assets = [[HBMyAssetsSlideViewController alloc] init];
HBBaseNavigationController *nav = [[HBBaseNavigationController alloc] initWithRootViewController:assets];
[self addChildVcItem:assets title:@"資產" image:@"TabBar5" selectedImage:@"TabBar5Sel"];
[self setViewControllers:@[[self.viewControllers objectAtIndex:0],[self.viewControllers objectAtIndex:1],nav,[self.viewControllers objectAtIndex:3]] animated:NO];