Search code examples
iosswift3uitabbarcontrollertabbarcontroller

How to Switch from one tabbar to another tabbar in Xcode 8.2.1


I want to know how to switch from one tab bar to another tab bar. Here is my story board I have two tab bars in story board But the problem is that I want to switch from My TabBar to MainTabBar

enter image description here

here is my code

 let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let MainTabBarController = storyBoard.instantiateViewController(withIdentifier: "MainTabBarController") as! MainTabBarController
        self.present(MainTabBarController , animated: true, completion: nil)

Solution

  • Since the question is different from where this code is taken:

    You need to change your rootViewController

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
    UIApplication.shared.keyWindow?.rootViewController = viewController
    

    If above doesn't work, try :

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
     let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
     appDelegate.window?.rootViewController = viewController