gpt4 book ai didi

ios - 没有 Storyboard 的应用程序启动-UIViewController仍然不可见

转载 作者:行者123 更新时间:2023-12-01 18:03:27 24 4
gpt4 key购买 nike

阅读https://medium.com/ios-os-x-development/ios-start-an-app-without-storyboard-5f57e3251a25之后
我已经执行了以下步骤

  • 从项目
  • 中删除 Main.storyboardLaunchScreen.storyboard
  • 下的主界面中删除部署信息
  • Main.storyboard
  • 中删除对 LaunchScreen.storyboardInfo.plist的引用

    这是上述步骤的一些屏幕截图
    enter image description here
    enter image description here
    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()
    }
    }
    }

    关于ios - 没有 Storyboard 的应用程序启动-UIViewController仍然不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63650161/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com