Ios 没有情节提要的应用程序启动-UIViewController仍然不可见

Ios 没有情节提要的应用程序启动-UIViewController仍然不可见,ios,swift,Ios,Swift,读后 我已经执行了以下步骤 从项目中删除Main.storyboard和LaunchScreen.storyboard 从部署信息下的主界面中删除主界面 从Info.plist 下面是上述步骤的一些屏幕截图 在AppDelegate.swift中,我做了以下修改 import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow?

读后

我已经执行了以下步骤

  • 从项目中删除
    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
        }
    }
    
    当我启动模拟器时,控制台上会打印“已执行的应用程序”

    我希望在应用程序启动后看到全屏红色。然而,我只能看到整个黑屏


    我可以知道我还遗漏了哪些步骤吗?

    您有场景生命周期,所以应该在
    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)
        }
    }
    
    现在是
    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()
            }
        }
    }
    

    您有场景生命周期,所以应该在
    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)
        }
    }
    
    现在是
    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()
            }
        }
    }