我有一个应用UITabBarController
作为我的初始视图控制器。
目前我在Storyboard中做了所有事情,但我想以编程方式根据用户登录或不登录的方式向标签栏添加标签。
我做了一个TestViewController
来测试它。现在我有两个标签(如下图所示)。我想以编程方式在右侧放置第三个选项卡。我将此代码放在AppDelegate的didFinishLaunching
方法中。基于print语句,视图控制器正被添加到选项卡栏,但它没有出现在选项卡栏中,然后应用程序加载。
有什么建议吗?
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)")
}
答案 0 :(得分: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()
答案 1 :(得分:0)
这适用于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()
答案 2 :(得分:0)
如果您不想使用UIStoryboard
,并且有三个名为oneVC
,twoVC
和threeVC
的视图控制器,则可以使用(在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()