Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Black screen when changing root view controller in AppDelegate

I am trying to change the root view controller from the app delegate's didFinishLaunchingWithOptions, depending on whether the user is logged in or not. Once I get past this condition, I am using the following code to change root view controllers:

self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController

self.window?.makeKeyAndVisible()

However, when I launch the app (with a valid logged in user) the simulator first shows the log in screen (old root view controller) for a second, then the screen goes black for about 30 seconds to a minute, before finally showing the desired view controller.

The view controller structure in storyboard is as follows:

SWRevealViewController -> Navigation Controller -> Desired View Controller (new root)

The reason for beginning with SWRevealViewController is because the slide menu is lost otherwise.

Any ideas of what might be going on?

like image 451
rodrigochousal Avatar asked Feb 04 '26 16:02

rodrigochousal


1 Answers

I found a way to produce a similar result to the one I desired. It involves not changing the root view controller at all, and having it present an "artificial root view controller" after launch:

if let currentRoot = self.window?.rootViewController {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let artificialRoot = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
    currentRoot.present(artificialRoot, animated: false, completion: nil)
}

Although not ideal, the results of this implementation are by far better than the implementation in the question description.

like image 51
rodrigochousal Avatar answered Feb 07 '26 05:02

rodrigochousal