【问题标题】:Programmatically add tab to tab bar以编程方式将选项卡添加到选项卡栏
【发布时间】:2016-12-30 19:45:52
【问题描述】:

我有一个带有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)")

}

【问题讨论】:

    标签: ios swift uitabbarcontroller uitabbar


    【解决方案1】:
        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()
    

    【讨论】:

      【解决方案2】:

      这是给 swift 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()
      

      【讨论】:

        【解决方案3】:

        如果您不想使用 UIStoryboard 并且如果您有三个名为 oneVCtwoVCthreeVC 的视图控制器,您可以使用(适用于 Swift 5.3 和 iOS 14.2)

                let window = UIWindow(frame: UIScreen.main.bounds)
                window.backgroundColor = .systemBackground
                self.window = window
        
                // Put image path if you want to have an image on your TabBar for this view controller
                self.oneVC?.tabBarItem = UITabBarItem(title: "One", image: nil, selectedImage: nil)
                self.twoVC?.tabBarItem = UITabBarItem(title: "Two", image: nil, selectedImage: nil)
                self.threeVC?.tabBarItem = UITabBarItem(title: "Three", image: nil, selectedImage: nil)
        
                let tabController = UITabBarController()
                tabController.viewControllers = [oneVC, twoVC, threeVC]
                tabController.selectedIndex = 0
        
                self.window!.rootViewController = tabController
                self.window?.makeKeyAndVisible()
        
        

        【讨论】:

          猜你喜欢
          • 2017-11-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-02
          • 1970-01-01
          相关资源
          最近更新 更多