Black screen when changing root view controller in AppDelegate

set root view controller programmatically swift 5
present view controller from appdelegate swift
change root view controller swift
uinavigationcontroller(rootviewcontroller)
change root view controller of uinavigationcontroller swift
set xib as root view controller swift
ios replace root view controller
scenedelegate black screen

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?


Here is an example, that works for me, just make your loginViewControllers to be a stack of UINavigationController, not related to SWRevealController. It is easy workaround.

self.window = UIWindow(frame: UIScreen.main.bounds)
if User.shared.firstLaunch {
     let navigationVC = Storyboard.inital.instantiateViewController(withIdentifier: "firstLaunchNC") as! UINavigationController
     self.window?.rootViewController = navigationVC
} else if User.shared.token == "" {
     let navigationVC = Storyboard.inital.instantiateViewController(withIdentifier: "initialVC") as! UINavigationController
     self.window?.rootViewController = navigationVC
} else {
     self.registerForPushNotifications(application: UIApplication.shared)
     User.shared.refreshToken()
     let revealController = Storyboard.main.instantiateViewController(withIdentifier: "revealViewController") as! SWRevealViewController
     self.window?.rootViewController = revealController
}

self.window?.makeKeyAndVisible()
return true

Followed tutorial and all I got was a black screen in the simulator , Followed tutorial and all I got was a black screen in the simulator in didFinishLaunchingWithOptions in the AppDelegate all I got was a black screen. or add it back if not using Storyboards to set the rootViewController. This exception is thrown if a view controller or the app returns 0 instead of a valid set of supported interface orientation values. It is also thrown if the orientation returned by a view controller’s preferred Interface Orientation For Presentation method does not match one of the view controller’s supported orientations.


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.

UIWindow results in a black screen when changing the - iOS, UIWindow results in a black screen when changing the rootViewController - ios. Btw. the code above is all there is in my AppDelegate and the issues persists  Splash screen to Authentication screen, when no active user session exists. Splash screen to Main screen, when the user session is valid. This is what will take place most of the time.


try this:

let viewController = ViewController()
let navigationController = UINavigationController(rootViewController: viewController)
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()

Set A Root View Controller Programmatically in Swift 5, ▻ Run the app and you will see the black screen and let's change that by setting a new root view controller. STEP #3 Set A Root View Controller. Open up  Import the view controller, then type out the following lines of code: LogInViewController * vc = [[ LogInViewController alloc ] init ]; UINavigationController * navigationController = [[ UINavigationController alloc ] initWithRootViewController : vc ]; self . window . rootViewController = navigationController ;


try this:

 UIStoryboard *storyboard  = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UITabBarController *tbc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarController"];
            tbc.selectedIndex=[singleton getSelectedTabIndex];
            // tbc.selectedIndex=0;
            //self.window.rootViewController = tbc;
            [self.window setRootViewController:tbc];

            [UIView transitionWithView:self.window duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil

completion:nil];

If This not work..Please check your initial Controller ...In Which Never try Load NSAtrributedString

NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];

Note:-- Black Screen Apear When Initial Controller ViewDidLoad Take Some time to load UIview into Memory due some Lazy UI loading Assests are done in ViewDidLoad...

Try heavy load view cide into ViewWillApear or ViewDidApear ....

Why I can't see Launch Screen when change Root |Apple , How can I see the `splashScreen(Launch Screen)` and go to the root view? **​Main. **Move Root Screen in AppDelegate.swift**. ~~~swift rootViewController = viewController; }; func switchLoginUI(_ pushtype : String) {; let loginStoryboard However, go to the root view after the black or white screen. A root view controller is assigned, the local window constant is assigned to the window property, and the window is “made key and visible”, i.e. put the window at the front of the app’s UI. Specific to SwiftUI, a ContentView is created, and added as a root view controller by making use of a UIHostingController. This controller is used to


Black Screen only in IOS 13 when i run my appli |Apple Developer , This is my app AppDelegate code: didDiscardSceneSessions sceneSessions: Set) {; // Called when the user discards a rootViewController = navigationView; self.window? See also applicationDidEnterBackground:. To build a single screen in an application, Views are stacked on top of each other in a Content View Hierarchy, and the hierarchy is managed by a single View Controller. Applications with multiple screens have multiple Content View Hierarchies, each with its own View Controller, and the application places Views in the Window to create a different Content View Hierarchy based on the screen that the user is on.


Black screen on simulator - iOSProgramming, Which version of iOS does this happen on? I had an issue where I was replacing the root view controller of a window which worked fine on 12 but not on 13. A few​  3- Let’s modify the AppDelegate file. We’ve defined our main controller (ViewController) then set as root view controller of UINavigationController. It means, your navigation controller will be set of our application’s root controller (window.rootViewController). Let’s start coding the mentioned application in above.


Black screen displayed briefly on iOS when app launches · Issue , Launch an iOS application with a light-colored launch screen two of total black is shown between the launch screen and the initial Flutter view. This is my workaround for now, in the app delegate: rootViewController; controller.view. flutter/engine@f7e0614 Change ParagraphBuilder to replace the  Changing the root view controller of a window. Changing the screen on which the window is displayed. Windows do not have any visual appearance of their own. Instead, a window hosts one or more views, which are managed by the window's root view controller. You configure the root view controller in your storyboards, adding whatever views are