【问题标题】:Black screen when changing root view controller in AppDelegate在 AppDelegate 中更改根视图控制器时出现黑屏
【发布时间】:2017-09-14 21:31:03
【问题描述】:

我正在尝试从应用程序委托的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 秒到一分钟,之前最终显示所需的视图控制器。

storyboard中的视图控制器结构如下:

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

之所以以SWRevealViewController开头是因为否则滑动菜单会丢失。

对可能发生的事情有任何想法吗?

【问题讨论】:

  • 你能设置rootViewController 之前你使窗口键可见吗?
  • @NRitH 结果一模一样。
  • 你能显示更多代码吗?查看您如何设置 loginViewController 以及触发您显示的此代码的原因会很有用。

标签: ios swift3 xcode8 swrevealviewcontroller


【解决方案1】:

这是一个适用于我的示例,只需将您的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

【讨论】:

    【解决方案2】:

    我找到了一种方法来产生与我想要的结果相似的结果。它涉及根本不更改根视图控制器,并在启动后让它呈现一个“人工根视图控制器”:

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

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

    【讨论】:

      【解决方案3】:

      试试这个:

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

      【讨论】:

        【解决方案4】:

        试试这个:

         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
        

        完成:无];

        如果这不起作用..请检查您的初始控制器 ...在其中永远不要尝试加载 NSAtrributedString

        NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] 选项:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 文档属性:无 错误:无];

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-18
          • 1970-01-01
          • 1970-01-01
          • 2019-03-06
          相关资源
          最近更新 更多