Programmatically Change Rootviewcontroller of Storyboard

Programmatically change rootViewController of storyBoard

Objective-C:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarcontroller"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];

Swift :

 let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("tabBarcontroller") as UITabBarController
UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;

Swift 3:

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

Swift 5:

let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()

Or simply like this:

let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()

Both works fine!

Change root ViewController programmatically

Set this in appDelegate's didFinishLaunchingWithOptions method according to current app settings , also you should use window not keyWindow

if(userExists)
{
let vc = storyboard.instantiateViewController(withIdentifier: "tabBarVC")

UIApplication.shared.window.first?.rootViewController = vc

}
else
{
let vc = storyboard.instantiateViewController(withIdentifier: "loginVC")

UIApplication.shared.window.first?.rootViewController = vc
}

Don't use present as this will automatically change the root

Also another way to access windows (used when AppDelegate has a value)

let appDelegate = UIApplication.shared.delegate as? AppDelegate
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
appDelegate?.window?.rootViewController = homeController

Change root View Controller

For 1) you can't present a view controller using a segue and then use it to replace the root view controller in the prepare. You will need to instantiate the tab view controller from the storyboard and then replace the root view controller.

Something like this:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "TabController")
UIApplication.shared.keyWindow?.rootViewController = vc

(assuming the storyboard is called 'Main' and you give the tab controller the storyboard ID of 'TabController'.

I'm not quite clear on what the issue is for 2.

However as a general note I would approach this differently and instead of having the login controller as your initial view controller have the tab bar as the initial controller and then just present the login controller the first time the app starts. That way you avoid replacing the root controller at all and it's all more controlled.

Set rootViewController programmatically not working, picking initial view controller from storyboard only in Xcode 11

Got the answer by doing some more google, i mean stackoverflow.

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
let storyboard = UIStoryboard(name: "Main", bundle: nil)

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let vc = storyboard.instantiateViewController (withIdentifier: "Primary") as! ViewController
window = UIWindow(windowScene: windowScene)
window?.rootViewController = vc
window?.makeKeyAndVisible()
}

SceneDelegate is used for this purpose since iOS 13 not AppDelegate. Here is the link SceneDelegate example

Programmatically set rootViewcontroller for UINavigationcontroller in Storyboard in Appdelegate

You can create an object of UINavigationController with your ViewController as per if/else condition and set the navigation controller as rootViewController property of window in AppDelegate as follows:

LoginViewController *loginController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"loginController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;

How to set root view controller in storyboard animated with navigation controller programmatically

User following method to set rootViewController using navigation:

[(UINavigationController*)self.window.rootViewController pushViewController:yourViewController animated:YES];

Storyboard's view controller not showing up when programmatically set as root view controller

You need to initialize the window programmatically.

Try adding the following to the beginning of your didFinishLaunchingWithOptions method in Appdelegate

self.window = UIWindow()

Also in your showPermissionVC and showContainerVC methods, replace this line:

UIApplication.shared.windows.first?.rootViewController = rootVC

with:

window?.rootViewController = rootVC


Related Topics



Leave a reply



Submit