ios - 如何在 xcode 11 中为新项目使用特定的根控制器初始化 uiwindow?
问题描述
我想在里面初始化窗口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
还从部署信息中删除了 Main
结果在设备上我看到黑屏,但调试器显示 rootNavigationController 应该是 来自调试器的图像
如何修复它或实现这个逻辑XCode11
?
解决方案
解决方案: 1)在 manifest(plist) 文件中删除 Storyboard Name 字段 2)在 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 中实现这个逻辑
推荐阅读
- python - 我的网站没有加载正确的 css 参数
- r - R应用结果不一致
- javascript - 为什么我不断收到 .addEventListener 不是函数?
- api - Steam Web API:我在哪里可以找到公开/私密的个人资料状态?
- python - 需要帮助了解为什么我对 find() 方法的使用没有正确执行
- c# - C# 中的算术运算应用程序中的错误
- java - 在 JVM 内长时间录制音频时突然延迟
- python - 我在 pythoneverywhere.com 上创建了一个数据库,我想在本地使用它,在 Visual Studio 代码项目中
- jquery - 我如何通过比例效果显示 div 并给出她的文字
- python - Python3中的简单测验程序