Rakesh
Rakesh

Reputation: 131

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

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.

Upvotes: 1

Views: 8583

Answers (3)

Suhit Patil
Suhit Patil

Reputation: 12023

You can create tabbarcontroller like below, using Xib

    //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
     }

Upvotes: 3

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

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

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

That means your root will be UITabBarController, then for each tab there will be UINavigationController whose first view controller will be again a 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.

enter image description here

Upvotes: 0

Amanpreet
Amanpreet

Reputation: 1321

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

enter image description here

Output is:

enter image description here

When you press button:

enter image description here

Upvotes: 4

Related Questions