iOS面试题iOS项目

XCode 11 新建项目之SceneDelegate(二)

2019-09-26  本文已影响0人  Just丶Go

继上篇文章 XCode 11 新建项目之SceneDelegate(一)
这篇将介绍自定义SceneDelegate相关的内容
同样文末附有Demo~

上一篇文章已经介绍自定义rootViewController
接下来接着上次的代码继续讲述

关键点还是在info.plist中的Application Scene Manifest字典中

1. 首先如果不使用`iOS 13`多窗口功能, 那么你只需要将该字典中的`Scene Configuration`删除掉, 同时保持
   `Enable Multiple Windows`的值为`NO`即可.

按照1中所述,这时就不会走AppDelegate.swiftUISceneSession Lifecycle相关的代理方法了.至此也不再需要进行额外配置!
补充 - 2019.9.27
但是此时仍然会走SceneDelegate中的代理方法,根控制器仍然在这里进行配置

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 = ALWindow(windowScene: windowScene)
        // setup root viewController
        window?.rootViewController = ALWindow.configurationRootViewController()
        
        window?.makeKeyAndVisible()
    }

**上述内容是针对不需要进行多窗口适配的项目的配置 ~**

接下来开始讲述需要进行多窗口适配的相关配置(简约版)

image.png

按照如上配置后,当前info.plist中的 Application Scene Manifest 的结构如上图所示。

显然
若需要使用多窗口特性,需要将Enable Multiple Windows 的值改为YES即可(即开启多窗口功能)
这时,AppDelegate.swiftUISceneSession Lifecycle的代理方法会被执行。代码如下

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {

        let sceneConfiguration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
        // 配置scene的代理类为 ALSceneDelegate
        // delegateClass 设置为自己自定义的sceneDelegate即可
        sceneConfiguration.delegateClass = ALSceneDelegate.classForCoder()
        // 这里如果需要,可以自定义UIWindowScene对象。
        // sceneConfiguration.sceneClass = UIWindowScene.classForCoder()
        return sceneConfiguration
    }

此章结束 ... ...

上一篇 下一篇

猜你喜欢

热点阅读