UITabBarController 如何更換里面的viewcontroller

最近遇到個需求
游客模式下 先加載好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];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,180評論 4 61
  • 體驗入:這幾天各部門的每個人都很關心業績的問題,距離20號就還有8天了,都怕完不成挨罰,大家都很用心的想要完成 找...
    Tracy_zhang閱讀 138評論 0 1
  • 我未入過繁華之境 未聽過喧囂的聲音 未見過太多生靈 未有過滾燙心情 所以也未覺大洋正中 有多么安靜 直到 遇見你
    正能量鎖骨少女閱讀 151評論 0 0