iOS 12/13程序化视图创建

时间:2019-10-09 04:08:30

标签: ios swift uiapplicationdelegate

我的应用程序不使用情节提要。所有视图都是以编程方式创建的。

从历史上看,我删除了title,从Main.storyboard中删除了引用,并按如下所示设置了Info.plistUIWindow

rootViewController

但是,当尝试在iOS 13中运行我的应用程序时,出现崩溃-

    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 12仍按预期运行。如何以编程方式设置视图以支持iOS 12和13?

1 个答案:

答案 0 :(得分:1)

您还需要添加更新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