How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?

时间:2021-03-02 07:36:03
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"

I am new to swift. I have configured the tab bar controller in appdelegate.Now I need to set the rootview controller here and I need to show the tabbar in all my viewcontrollers that I declare.

我是新手。我在appdelegate中配置了标签栏控制器。现在我需要在这里设置rootview控制器,我需要在我声明的所有viewcontrollers中显示标签栏。

3 个解决方案

#1


4  

This will help you. Try to set navigation inside of tab bar controller. Give tab bar item to navigation controller. Like:

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

How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?

Output is:

How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?

When you press button:

按下按钮时:

How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?

#2


3  

You can create tabbarcontroller like below, using Xib

您可以使用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
     }

#3


0  

As per my understanding, you want to achieve something like this :

根据我的理解,你想要实现这样的事情:

UITabBarController --> for each tab there will be a navigation controller whose Root will be a VC having tab bar

UITabBarController - >对于每个选项卡,将有一个导航控制器,其Root将是一个具有标签栏的VC

So I would suggest you to directly use UITabBarControllers for your each root of navigation controller.

所以我建议你直接使用UITabBarControllers为你的每个导航控制器根。

That means your root will be UITabBarController, then for each tab there will be UINavigationController whose first view controller will be again a UITabBarController.

这意味着你的root将是UITabBarController,然后对于每个选项卡,将有UINavigationController,其第一个视图控制器将再次成为UITabBarController。

For more understanding see below figure. It's only showing flow for one tab of UITabBarController. Repeat the same for all your other tabs.

有关更多的理解,请参见下图。它只显示UITabBarController的一个选项卡的流程。对所有其他选项卡重复相同的操作。

How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?

#1


4  

This will help you. Try to set navigation inside of tab bar controller. Give tab bar item to navigation controller. Like:

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

How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?

Output is:

How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?

When you press button:

按下按钮时:

How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?

#2


3  

You can create tabbarcontroller like below, using Xib

您可以使用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
     }

#3


0  

As per my understanding, you want to achieve something like this :

根据我的理解,你想要实现这样的事情:

UITabBarController --> for each tab there will be a navigation controller whose Root will be a VC having tab bar

UITabBarController - >对于每个选项卡,将有一个导航控制器,其Root将是一个具有标签栏的VC

So I would suggest you to directly use UITabBarControllers for your each root of navigation controller.

所以我建议你直接使用UITabBarControllers为你的每个导航控制器根。

That means your root will be UITabBarController, then for each tab there will be UINavigationController whose first view controller will be again a UITabBarController.

这意味着你的root将是UITabBarController,然后对于每个选项卡,将有UINavigationController,其第一个视图控制器将再次成为UITabBarController。

For more understanding see below figure. It's only showing flow for one tab of UITabBarController. Repeat the same for all your other tabs.

有关更多的理解,请参见下图。它只显示UITabBarController的一个选项卡的流程。对所有其他选项卡重复相同的操作。

How to set rootview in tabbar controller and show tabbar in each view controller in swift ios?