iOS 13 开始,SceneDelegate
取代了原来 AppDelegate
里的大部分代理方法,如果 App 最低系统要求是 iOS 13,那么就不会走原来 AppDelegate
里的代理方法了。
对应地,原来在 didFinishLaunchingWithOptions
里创建 Key Window 的逻辑也需要移到 SceneDelegate 中:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
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)
window?.frame = windowScene.coordinateSpace.bounds
window?.rootViewController = UINavigationController(rootViewController: ViewController())
window?.makeKeyAndVisible()
}
}
主要有两点区别:
- 使用
windowScene.coordinateSpace.bounds
替代UIScreen.main.bounds
作为 Window 的frame
。 - Window 必须关联一个
windowScene
,否则无法显示。