【问题标题】:Black when screen when settings initial controller programmatically以编程方式设置初始控制器时屏幕变黑
【发布时间】:2019-11-08 14:47:43
【问题描述】:

请你看看 repo https://github.com/Rukomoynikov/InitialViewControllerProgrammatically 并帮助我。为什么我在尝试实例化ViewController 时出现黑屏。

这是我的 AppDelegate:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow.init(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        guard window != nil else { return true }
        self.window!.backgroundColor = .darkGray
        self.window!.rootViewController = viewController
        self.window!.makeKeyAndVisible()

        return true
    }
}

情侣详情。

  1. 在上一个 Xcode 版本中创建的应用程序。
  2. iOs 部署目标从 13 更改为 12。
  3. SceneDelegate 已移除。
  4. 在目标设置选项中主界面已清除。
  5. 在 info.plist 中 StoryBoardName 和 DelegateClassName 也被删除。

【问题讨论】:

    标签: swift uikit


    【解决方案1】:

    问题在于,为了使项目停止使用场景委托并改用应用委托,您破坏了 Info.plist 中的 UIApplicationSceneManifest 条目。相反,您需要完全删除该条目。它的存在就是导致使用场景委托架构的原因。

    不过,最好在 iOS 12 上使用应用程序委托和在 iOS 13 上使用场景委托(正如我在 https://stackoverflow.com/a/58405507/341994 中描述的那样)。

    【讨论】:

      【解决方案2】:

      iOS 13 已将 Windows 设置从 AppDeleagte 移至 SceneDelegate,以支持使用(可能是多个)场景而不是单个窗口。您现在必须像这样进行设置:

      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,因为我想从 12 开始支持 iOS。
      • @MaksimRukomoynikov 不,你没有删除它。它就在你的 github 项目中;你还破坏了 info.plist 的项目。
      • @matt 谢谢。但这只是参考(我也从新提交的文件中删除了它)
      • 但这就是现在的全部问题。你破坏了UIApplicationSceneManifest 条目。您需要将其完全删除。
      • @matt 哦,非常感谢。我从 info.plist 中删除了(Application Scene Manifest),它开始按预期工作。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多