在 AppDelegate 中更改根视图控制器时出现黑屏

Black screen when changing root view controller in AppDelegate

我正在尝试根据用户是否登录来更改应用委托 didFinishLaunchingWithOptions 的根视图控制器。一旦我克服了这种情况,我将使用以下代码来更改根视图控制器:

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

self.window?.makeKeyAndVisible()

但是,当我启动应用程序(使用有效的登录用户)时,模拟器首先显示登录屏幕(旧的根视图控制器)一秒钟,然后屏幕变黑大约 30 秒到一分钟, 在最终显示所需的视图控制器之前。

故事板中的视图控制器结构如下:

SWRevealViewController -> 导航控制器 -> 所需的视图控制器(新根)

之所以以SWRevealViewController开头是因为否则幻灯片菜单丢失了。

对可能发生的事情有什么想法吗?

这是一个对我有用的例子,只是让你的 loginViewControllers 成为一堆 UINavigationController,与 SWRevealController 无关。解决方法很简单。

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

我找到了一种方法来产生与我想要的结果相似的结果。它涉及根本不更改根视图控制器,并让它在启动后显示 "artificial root view controller":

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)
}

虽然不理想,但是这个实现的结果比问题描述中的实现要好很多。

试试这个:

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

试试这个:

 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];

如果这不起作用..请检查您的初始控制器...在其中切勿尝试加载 NSAtributedString

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

注意:-- 初始控制器 ViewDidLoad 时出现黑屏需要一些时间将 UIview 加载到内存中,因为一些延迟 UI 加载资产在 ViewDidLoad 中完成...

尝试将视图 cide 重载到 ViewWillApear 或 ViewDidApear ....