iOS 12 / 13 编程视图创建

iOS 12 / 13 Programmatic View Creation

我的应用程序不使用故事板。所有视图均以编程方式创建。

从历史上看,我已经删除了我的 Main.storyboard,从我的 Info.plist 中删除了引用,并按如下方式设置了我的 UIWindowrootViewController

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = UIViewController()
        window.makeKeyAndVisible()

        self.window = window

        return true
    }

然而,在 iOS 13 中尝试 运行 我的应用程序时,我遇到了崩溃 -

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle NSBundle </Users/Dev/Library/Developer/CoreSimulator/Devices/8A4474B1-FCA3-4720-8F34-A6975A03EE19/data/Containers/Bundle/Application/258FA246-A283-4FE6-A075-58BD32424427/Home.app> (loaded)'
***

iOS 12 仍然是 运行 的预期。我应该如何以编程方式设置我的视图以支持 iOS 12 和 13?

您还需要添加更新 SceneDelegate.swift

更新 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) 并添加

        guard let windowScene = (scene as? UIWindowScene) else { return }
        let window = UIWindow(windowScene: windowScene)

        let viewController = ViewController()
        window.rootViewController = viewController
        window.makeKeyAndVisible()

        self.window = window