能否请您查看仓库https://github.com/Rukomoynikov/InitialViewControllerProgrammatically并为我提供帮助。为什么在尝试实例化ViewController时出现黑屏。
这是我的AppDelegate:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow.init(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
guard window != nil else { return true }
self.window!.backgroundColor = .darkGray
self.window!.rootViewController = viewController
self.window!.makeKeyAndVisible()
return true
}
}
夫妇的详细信息。
答案 0 :(得分:1)
问题是,为了使项目停止使用场景委托而改为使用应用程序委托,您对 Info.plist 中的UIApplicationSceneManifest
项进行了修改。相反,您需要全部删除该条目。它的存在是导致使用场景委托体系结构的原因。
但是,最好在使用应用程序委托的iOS 12和使用场景委托的iOS 13上都做到这一点(正如我在https://stackoverflow.com/a/58405507/341994所述)。
答案 1 :(得分:0)
iOS 13已将窗口设置从AppDeleagte移至SceneDelegate,以支持使用(可能是多个)场景而不是单个窗口。现在,您必须像这样进行设置:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let vc = storyboard.instantiateViewController (withIdentifier: "Primary") as! ViewController
window = UIWindow(windowScene: windowScene)
window?.rootViewController = vc
window?.makeKeyAndVisible()
}
}