美文网首页iOS学习iOS面试题iOS项目
XCode 11 新建项目之SceneDelegate(一)

XCode 11 新建项目之SceneDelegate(一)

作者: Just丶Go | 来源:发表于2019-08-15 20:27 被阅读0次

    因为在 iOS 13中, 新增特性 分屏 功能,所以现在新建项目都会包含一个名为SceneDelegate的文件。

    文末附有Demo

    并且在AppDelegate文件中找不到属性window,最后发现 window属性被移到了SceneDelegate中。

    那么问题来了~

    Q: 在APP启动时,我想使用自定义的根控制器,但是突然不知道怎么做了!!!
    现在记录下如何自定义

    1. 首先找到 info.plist 文件,找到 Key 为`Application Scene Manifest`的字典.
    该字典结构如下:
    `Application Scene Manifest = 
    {
      Enable Multiple Windows : NO,
      Scene Configuration : {
    Application Session Role : [
                                Configuration Name : Default Configuration,
                                Delegate Class Name : $(PRODUCT_MODULE_NAME).SceneDelegate,
                                Storyboard Name : Main
                                ]
                               }
    }`
    

    如图:

    image.png
    2. 将 `Storyboard Name : Main` 这一栏删掉,如果只删掉 `Main` 而不删掉 Key,APP启动时还是会去找`storyboar`,从而报错。
    
    3. 界面初始化的操作要到 `SceneDelegate` 中进行设置,和以前在`didFinishLaunchingWithOptions`中一样。
       代码如下:
    
    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 }
            
            let vc = ViewController()
            let nc = UINavigationController(rootViewController: vc)
    
            // Create the window. Be sure to use this initializer and not the frame one.
            window = UIWindow(windowScene: windowScene)
            window?.rootViewController = nc
            window?.makeKeyAndVisible()
        }
    

    PS:需要注意的是,初始化window时,要用windowScene:这个方法拿到windowScene来初始化,否则初始化后的界面是黑的。

    Demo--> Demo地址

    相关文章

      网友评论

        本文标题:XCode 11 新建项目之SceneDelegate(一)

        本文链接:https://www.haomeiwen.com/subject/jczzjctx.html