func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.white
let tabBarController = self.window!.rootViewController as! UITabBarController
let tabBar = tabBarController.tabBar as UITabBar
let tabBarItem0 = tabBar.items![0] as! UITabBarItem
let tabBarItem1 = tabBar.items![1] as! UITabBarItem
let tabBarItem2 = tabBar.items![2] as! UITabBarItem
let tabBarItem3 = tabBar.items![3] as! UITabBarItem
tabBar.barTintColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
tabBarItem0.title = "Home"
tabBarItem1.title = "Search"
tabBarItem2.title = "User"
我是swift的新手。我在appdelegate中配置了标签栏控制器。现在我需要在这里设置rootview控制器,我需要在我声明的所有viewcontrollers中显示标签栏。
答案 0 :(得分:4)
答案 1 :(得分:3)
您可以使用Xib
创建如下所示的tabbarcontroller //MARK: didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarController = UITabBarController()
let tabViewController1 = FirstTabViewController(nibName: "FirstTabViewController", bundle: nil)
let tabViewController2 = SecondViewController(nibName:"SecondViewController", bundle: nil)
tabViewController1.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "home_icon"),tag: 1)
tabViewController2.tabBarItem = UITabBarItem(title: "Search",image:UIImage(named: "search_icon") ,tag:2)
tabBarController.viewControllers = [tabViewController1,tabViewController2]
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
答案 2 :(得分:0)
根据我的理解,你想要实现这样的目标:
UITabBarController
- >对于每个标签,将有一个navigation controller
,其根将是VC
标签栏
因此,我建议您直接使用UITabBarControllers
作为navigation controller
的每个根。
这意味着您的根将是UITabBarController
,然后对于每个tab
,UINavigationController
的第一个view controller
将再次为UITabBarController
。
有关更多说明,请参见下图。它只显示UITabBarController
的一个标签的流量。对所有其他tabs
重复相同的操作。