0
votes

I have been teaching my self how to develop and I got to the point to where I need guidance from experts. I created some of the user interface programmatically using the MVC structure. My question is how can I embed my navigation controller into my tab bar controller so my tab bar controller can be on every screen. I made the tab bar controller in main.storyboard and referenced it in the View controller I named Home Controller.

    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    let tabViewController = storyboard.instantiateViewController(withIdentifier: "TabBar")

    self.present(tabViewController, animated: true, completion: nil)

The code above is in the view did load function. I was wondering do I need to change up the root view controller in my app delegates?

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let layout = UICollectionViewFlowLayout()      
    window?.rootViewController =  UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))

This is in my finished launched func.

2

2 Answers

0
votes

Use following lines

var navigationController = UINavigationController(rootViewController: viewController));

tabBarController.viewControllers = [navigationController,firstViewControllersecondViewController, ]
0
votes

create a separate UITabBarController class and initialize it in AppDelegate like this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
      window = UIWindow(frame: UIScreen.main.bounds)

      window?.rootViewController = MyTabBarController()

      window?.makeKeyAndVisible()

      return true
}

and the custom tabBarController

import UIKit

class MyTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()


        self.tabBar.barTintColor = UIColor.black
        self.tabBar.tintColor = UIColor.white
        self.tabBar.unselectedItemTintColor = UIColor.white.withAlphaComponent(0.4)

        let firstViewController = FirstViewController()

        let firstViewTabBarItem = UITabBarItem(title: "First", image: UIImage(named: "calculator"), selectedImage: UIImage(named: "calculator"))

        firstViewController.tabBarItem = firstViewTabBarItem
        firstViewController.tabBarItem.tag = 0

        let historyViewController = HistoricDataViewController()

        historyViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .history, tag: 1)

        let tabBarList = [calculateViewController, historyViewController]

        viewControllers = tabBarList.map{UINavigationController(rootViewController: $0)}

    }

}

Hope this example helps.