删除Main Storyboard之后。以向后兼容的方式通过编程方式添加StoryBoard

问题描述 投票:0回答:1

我的应用目标为iOS 10.0。一旦更改目标,我的SceneDelegate文件中就会出现很多错误。为了向后兼容,我为Scene Delegate类添加了“ @available(iOS 13.0,*)”。

我从OnboardingController开始。这完全是程序性的。因此,我删除了MainStoryboard,并将其从信息中的“主界面”和“应用场景清单”中删除。

现在,我必须在AppDelegate和SceneDelegate中都设置rootView。如果我未在SceneDelegate中设置窗口,则在iOS 13.0+设备中我只会得到黑屏,如果我未在AppDelegate中设置,我会在<13.0设备中得到只有黑屏因为我同时调用了两个文件,所以我在Viewcontroller中的viewdidload()被调用了两次。

以下是我的AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)



        let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController

        window?.rootViewController = viewController
        window?.makeKeyAndVisible()
        return true
    }
}

以下是我的SceneDelegate

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {


    var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let windowScene = (scene as? UIWindowScene) else { return }          
            window = UIWindow(frame: windowScene.coordinateSpace.bounds)
            window?.windowScene = windowScene

            let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeCollectionVC") as! HomeCollectionVC
            window?.rootViewController = viewController
            window?.makeKeyAndVisible()

}
...
}

我想念东西吗?

ios swift xcode swift3 swift4
1个回答
0
投票

可以通过搜索轻松找到,但这是一个示例:

SceneDelegate.swift

import UIKit

// entire class is iOS 13+
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene

        let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeCollectionVC") as! HomeCollectionVC
        window?.rootViewController = viewController
        window?.makeKeyAndVisible()

    }

    func sceneDidDisconnect(_ scene: UIScene) {
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
    }

    func sceneWillResignActive(_ scene: UIScene) {
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
    }

}

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window : UIWindow?
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?)
        -> Bool {
            if #available(iOS 13, *) {
                // do only pure app launch stuff, not interface stuff
            } else {
                self.window = UIWindow()

                let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController

                window?.rootViewController = viewController
                window?.makeKeyAndVisible()

            }
            return true
    }

    // MARK: UISceneSession Lifecycle

    // iOS 13+ only
    @available(iOS 13.0, *)
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    // iOS 13+ only
    @available(iOS 13.0, *)
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}
© www.soinside.com 2019 - 2024. All rights reserved.