iOS 13 Xcode11变化 项目迁移准备

2019-10-15  本文已影响0人  iOS虞

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处理了,所以不能在AppDelegatefunc 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
上一篇下一篇

猜你喜欢

热点阅读