第一種簡單的(就在AppDelegate里的)
??????? let vc1 = ViewController()
??????? let nav1 = UINavigationController(rootViewController: vc1)
??????? nav1.tabBarItem = UITabBarItem(title: "", image: UIImage(named: ""), selectedImage: UIImage(named: ""))
???????
??????? let vc2 = erViewController()
??????? let nav2 = UINavigationController(rootViewController: vc2)
??????? nav2.tabBarItem = UITabBarItem(title: "", image: UIImage(named: ""), selectedImage: UIImage(named: ""))
???????
???????
???????
??????? let tbv = UITabBarController()
??????? tbv.viewControllers = [nav1,nav2]
??????? //tbv.selectedViewController = nav2
??????? self.window?.rootViewController = tbv
第二種麻煩的
第一步
創(chuàng)建MainViewController(UITabBarController)歌粥、FirstViewController酬滤、SecondViewController租幕、ThirdViewController、FourthViewController圃伶、AddViewController六個類,其中MainViewController繼承于UITabBarController鱼响,AddViewController可以是ViewController也可以是個View阱当。
AppDelegate里
self.window?.rootViewController = mainViewController()
mainViewController里
viewDidLoad()里:
self.addChildVC(childVC: firstViewController(), childTitle: "First", imageName: "xiaoXi", selectedImageName: "xiaoXi_H")
? ? ? ? self.addChildVC(childVC: secondViewController(), childTitle: "Second", imageName: "", selectedImageName: "")
? ? ? ? self.addChildVC(childVC: thirdViewController(), childTitle: "third", imageName: "", selectedImageName: "")
/// 添加子控制器
? ? private func addChildVC(childVC: UIViewController, childTitle: String, imageName: String, selectedImageName:String)
? ? {
? ? ? ? let navigation = UINavigationController(rootViewController: childVC)
? ? ? ? navigation.navigationBar.tintColor = UIColor.white //item 字體顏色
? ? ? ? navigation.navigationBar.barTintColor = UIColor.yellow //背景顏色
? ? ? ? childVC.title = childTitle
? ? ? ? childVC.tabBarItem.tag = 1
? ? ? ? childVC.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
? ? ? ? childVC.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.withRenderingMode(.alwaysOriginal)
? ? ? ? self.addChildViewController(navigation)
? ? }
最后是這樣: