iosswiftuitabbarcontrollerrootviewcontroller

How to change the rootviewcontroller to the third viewcontroller in a stack


var window: UIWindow?



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let tabBarController = UITabBarController()

        let settingStoryboard = UIStoryboard(name: "Setting", bundle: nil)
        let toDoListStoryboard = UIStoryboard(name: "ToDoList", bundle: nil)
        let homeStoryboard = UIStoryboard(name: "Home", bundle: nil)
        let eventListStoryboard = UIStoryboard(name: "EventList", bundle: nil)
        let activityStoryboard = UIStoryboard(name: "Activity", bundle: nil)

        let settingVC = settingStoryboard.instantiateViewController(withIdentifier: "Setting") as! SettingViewController
        let toDoListVC = toDoListStoryboard.instantiateViewController(withIdentifier: "ToDoList") as! ToDoListViewController
        let homeVC = homeStoryboard.instantiateViewController(withIdentifier: "Home") as! HomeViewController
        let eventListVC = eventListStoryboard.instantiateViewController(withIdentifier: "EventList") as! EventListViewController
        let activityVC = activityStoryboard.instantiateViewController(withIdentifier: "Activity") as! ActivityViewController

        let vcData: [(UIViewController, UIImage, UIImage)] = [

            (settingVC, UIImage(named: "SettingIcon")!, UIImage(named: "SettingSelectedIcon")!),
            (toDoListVC, UIImage(named: "ToDoIcon")!, UIImage(named: "ToDoSelectedIcon")!),
            (homeVC, UIImage(named: "HomeIcon")!, UIImage(named: "HomeSelectedIcon")!),
            (eventListVC, UIImage(named: "EventIcon")!, UIImage(named: "EventSelectedIcon")!),
            (activityVC, UIImage(named: "ActivityIcon")!, UIImage(named: "ActivitySelectedIcon")!)
        ]

        let vcs = vcData.map { (vc, image, selectedImage) -> UINavigationController in

            let nav = UINavigationController(rootViewController: vc)

            nav.tabBarItem.image = image.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            nav.tabBarItem.selectedImage = selectedImage.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)

            nav.tabBarItem.imageInsets = UIEdgeInsets(top: 7, left: 0, bottom: -5, right: 0)

            return nav
        }

        tabBarController.viewControllers = vcs
        tabBarController.tabBar.isTranslucent = false
        tabBarController.tabBar.barTintColor = UIColor(red: 19/255, green: 41/255, blue: 77/255, alpha: 1)

        UINavigationBar.appearance().barTintColor = UIColor(red: 19/255, green: 41/255, blue: 77/255, alpha: 1)
        UINavigationBar.appearance().isTranslucent = false

        window?.rootViewController = tabBarController

        print(Realm.Configuration.defaultConfiguration.fileURL!)

        return true
    }

In the code shown above when the app runs the rootviewcontroller is the first view that was added to the stack. How would you change the rootviewcontroller to the HomeViewController without initializing the homeviewcontroller first.


Solution

  • Just add

    tabBarController.selectedIndex = 2
    

    after your line

    tabBarController.viewControllers = vcs