iOS 13中的isKeyWindow属性始终返回nil是由于iOS 13引入了SceneDelegate来管理应用程序的多个窗口场景。在iOS 13之前,UIApplication的keyWindow属性可以用来获取应用程序的主窗口,但在iOS 13中,keyWindow属性已经被废弃。
在iOS 13及更高版本中,可以通过SceneDelegate来获取应用程序的主窗口。可以通过以下步骤来获取主窗口:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
window.rootViewController = YourRootViewController() // 替换为你的根视图控制器
window.makeKeyAndVisible()
self.window = window
在上述代码中,我们创建了一个新的UIWindow对象,并将其设置为应用程序的主窗口。然后,我们设置了根视图控制器,并使其可见。最后,我们将新创建的窗口赋值给SceneDelegate的window属性。
通过上述步骤,我们可以在iOS 13中获取应用程序的主窗口。但需要注意的是,如果应用程序中使用了多个窗口场景,可能需要根据具体的需求进行适当的调整。
腾讯云相关产品和产品介绍链接地址:
请注意,以上链接仅供参考,具体的产品选择应根据实际需求进行评估和决策。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云