没有故事板的应用程序启动 - UIViewController 仍然不可见

Che*_*eng 1 ios swift

阅读后https://medium.com/ios-os-x-development/ios-start-an-app-without-storyboard-5f57e3251a25

我已执行以下步骤

  1. 从项目中删除Main.storyboardLaunchScreen.storyboard
  2. 从“部署信息”下的主界面中删除“Main”
  3. Main.storyboard删除对和LaunchScreen.storyboard来自的引用Info.plist

这是上述步骤的一些屏幕截图

在此输入图像描述

在此输入图像描述

AppDelegate.swift,我做了以下修改

import UIKit

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

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.main.bounds)
        let viewController = ViewController()
        viewController.view.backgroundColor = UIColor.red
        window!.rootViewController = viewController
        window!.makeKeyAndVisible()
        
        print("application executed")
        
        return true
    }
}
Run Code Online (Sandbox Code Playgroud)

当我启动模拟器时,控制台上会打印“已执行的应用程序”。

我预计应用程序启动后会看到全屏红色。但是,我只能看到整个黑屏。

我可以知道我还错过了哪些步骤吗?

Asp*_*eri 5

您有场景生命周期,因此应该在SceneDelegate.

这是AppDelegate

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
}
Run Code Online (Sandbox Code Playgroud)

现在这里是SceneDelegate


class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        if let windowScene = scene as? UIWindowScene {
            let viewController = ViewController()
            viewController.view.backgroundColor = UIColor.red

            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = viewController
            self.window = window
            window.makeKeyAndVisible()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)