我想初始化appDelegate
内部的窗口以显示特定的ViewController
,这取决于某些情况。现在我有了这段代码:
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()
let rootNavigationController = UIViewController()
window?.rootViewController = rootNavigationController
window?.rootViewController?.view.backgroundColor = .green
window?.makeKeyAndVisible()
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
我正在使用XCode11
,并创建了新项目。我删除的SceneDelegate
文件对此没有影响。还从Info.plist
和部署信息
作为设备上的结果,我看到黑屏,但调试器向我显示了rootNavigationController应该是image from debugger
如何解决它或为XCode11
实现此逻辑?
解决方案:1)Inside manifest(plist) file remove Storyboard Name field2)在SceneDelegate.swift工具内部:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let rootNavigationController = UIViewController()
window?.rootViewController = rootNavigationController
window?.rootViewController?.view.backgroundColor = .green
window?.makeKeyAndVisible()
}
并且它将适用于ios 13,如果您希望支持ios 12及更低版本,还需要在AppDelegate中实现此逻辑