iOS 12/13 程序化 View 创建

标签 ios swift uiapplicationdelegate

我的应用程序不使用 Storyboard。所有 View 均以编程方式创建。

过去,我已经删除了我的 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 仍按预期运行。我应该如何以编程方式设置我的 View 以支持 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

关于iOS 12/13 程序化 View 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58296932/

相关文章:

ios - 使用 NSOperationQueue 执行 iOS 应用程序初始化

ios - 打开ViewController(它是tabBarController中的navigationController)并运行函数

ios - WKWebView 检查Web View 是否支持加载文件

javascript - 如何在 ios 手机 phonegap 编程中处理呼出和呼入电话的应用程序?

ios - 什么是 "dequeue cell"

ios - UIScrollView 无法水平滚动

swift - UITableViewAutomaticDimension 及其高度

ios - iOS 应用程序的 Xcode 验证失败

swift - "final let"在 Swift 中的用途,不是多余的吗?

ios - 'performFetchWithCompletionHandler' 是在没有网络连接的情况下调用的吗?