如何在tabbar控制器中设置rootview并在swift ios中的每个视图控制器中显示tabbar?

时间:2017-01-11 05:00:22

标签: ios swift3 uitabbarcontroller

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中显示标签栏。

3 个答案:

答案 0 :(得分:4)

这会对你有所帮助。尝试在标签栏控制器内设置导航。将标签栏项目提供给导航控制器。像:

enter image description here

输出是:

enter image description here

按下按钮时:

enter image description here

答案 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,然后对于每个tabUINavigationController的第一个view controller将再次为UITabBarController

有关更多说明,请参见下图。它只显示UITabBarController的一个标签的流量。对所有其他tabs重复相同的操作。

enter image description here