在TabbarController中为特定索引设置ViewController

时间:2017-07-06 12:42:12

标签: ios iphone swift3 uitabbarcontroller

我有一个UITabbarController,其中5 ViewControllers为索引号0,1,2,3,4,现在,如果它在iPhone中运行,那么加载了5个viewcontrollers,那很好,但是如果我将iPhone更改为iPad,索引号4应该不同ViewControllers

前。

if iPad{
    tabbarcontroller's index 4 should be B Viewcontroller
}else{
    tabbarcontroller's index 4 should be A Viewcontroller
}

我在目标c link here得到了答案,但我无法为Swift做出答案,这是我的代码:

let settingVc = self.storyboard?.instantiateViewController(withIdentifier: "SettingTabbarController") as! SettingTabbarController

var controllers : NSMutableArray =  [self.viewControllers!][0] as! NSMutableArray

print(controllers)

controllers.replaceObject(at: 4, with: settingVc)

self.setViewControllers(controllers, animated: true)

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

您可以在此处更改viewController以获取特定索引,如下所示

UITabbarController

   self.viewControllers?.remove(at: 4)
   let newItemController = UIStoryboard(name: "Settings", bundle: nil).instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
   newItemController.title = "Settings"
   newItemController.tabBarItem.image = UIImage(named: "ic_tab_settings")
   newItemController.tabBarItem.isEnabled = true
   self.viewControllers?.insert(newItemController, at:4)