2016-08-23 175 views
1

我有一個應用程序UITabBarController作爲我的初始視圖控制器。以編程方式將標籤添加到標籤欄

目前我在Storyboard中做所有事情,但我想以編程方式向基於用戶登錄或不登錄的選項卡欄添加選項卡。

我做了個TestViewController來測試一下。現在我有兩個選項卡(如下圖)。我想要以編程方式在右側放置第三個選項卡。我把這段代碼放在我的AppDelegate的didFinishLaunching方法中。基於打印語句,視圖控制器被添加到標籤欄,但它沒有出現在標籤欄中,然後加載應用程序。

有什麼建議嗎?

func addTabTEST() { 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let tabController = storyboard.instantiateViewControllerWithIdentifier("RootTabController") as! UITabBarController 
    let TestVC = storyboard.instantiateViewControllerWithIdentifier("TestViewController") as! TestViewController 
    let icon = UITabBarItem(title: "test", image: nil, selectedImage: nil) 
    TestVC.tabBarItem = icon 

    print("TAB CONTROLLERS 1: \(tabController.viewControllers)") 

    tabController.addChildViewController(TestVC) 
    tabController.viewControllers![2] = TestVC 

    print("TAB CONTROLLERS 2: \(tabController.viewControllers)") 

} 

enter image description here

enter image description here

回答

2
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 
    let nav1 = UINavigationController() 
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 

    let first: ViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController 
    nav1.viewControllers = [first] 
    nav1.setNavigationBarHidden(true, animated: true) 
    nav1.title = "first" 

    let nav2 = UINavigationController() 
    let second: SecondViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController 
    nav2.viewControllers = [second] 
    nav2.setNavigationBarHidden(true, animated: true) 
    nav2.title = "second" 

    let nav3 = UINavigationController() 
    let third: ThirdViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ThirdViewController") as! ThirdViewController 
    nav3.viewControllers = [third] 
    nav3.setNavigationBarHidden(true, animated: true) 
    nav3.title = "third" 



    let tabController = UITabBarController() 
    tabController.viewControllers = [nav1,nav2,nav3] 
    tabController.selectedIndex = 0 

    self.window!.rootViewController = tabController 
    self.window?.makeKeyAndVisible() 
0

這是迅速4

self.window = UIWindow(frame: UIScreen.main.bounds) let nav1 = UINavigationController() let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let first = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController 
    nav1.viewControllers = [first] 
    nav1.setNavigationBarHidden(true, animated: true) 
    nav1.title = "first" 

    let nav2 = UINavigationController() 
    let second: HomeViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController2") as! HomeViewController2 
    nav2.viewControllers = [second] 
    nav2.setNavigationBarHidden(true, animated: true) 
    nav2.title = "second" 

    let nav3 = UINavigationController() 
    let third = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController3") as! HomeViewController3 
    nav3.viewControllers = [third] 
    nav3.setNavigationBarHidden(true, animated: true) 
    nav3.title = "third" 



    let tabController = UITabBarController() 
    tabController.viewControllers = [nav1,nav2,nav3] 
    tabController.selectedIndex = 0 

    self.window!.rootViewController = tabController 
    self.window?.makeKeyAndVisible() 
相關問題