1.创建根视图控制器
Xcode11之前
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let rootvc = RootViewController()
self.window.rootViewController = rootvc
self.window.makeKeyAndVisible()
return true
}
Xcode11
QQ20191015-211731.png报错了
Use of unresolved identifier 'window'
,查看代码发现Xcode11AppDelegate.swift
中删除了var window: UIWindow?
之后都由SceneDelegate
处理了,所以不能在AppDelegate
的func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
创建根视图控制器了
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowscene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowscene)
window?.frame = UIScreen.main.bounds
let rootvc = RootViewController()
let nav = UINavigationController(rootViewController: rootvc)
window?.rootViewController = nav
window?.makeKeyAndVisible()
}
2.获取UIWindow
Xcode11之前
let window = UIApplication.shared.keyWindow
Xcode11
UIApplication.shared.windows.first
或
for scene in UIApplication.shared.connectedScenes {
let windowscene: UIWindowScene = scene as! UIWindowScene
if windowscene.activationState == .foregroundInactive {
let window = windowscene.windows.first!
break
}
}
或
let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window
网友评论