После удаления Main Storyboard.Add StoryBoard программно с обратной совместимостью - PullRequest
0 голосов
/ 27 марта 2020

Моя цель для приложения - iOS 10.0. Как только я меняю свою цель, я получаю кучу ошибок в файле SceneDelegate. Для обратной совместимости я добавил «@available (iOS 13.0, *)» для класса делегата сцены.

Я начал с OnboardingController. Который полностью запрограммирован c. Поэтому я удалил MainStoryboard и удалил его из «Основного интерфейса» и «Манифеста сцены приложения» в информации.

Теперь я должен установить rootView в AppDelegate и SceneDelegate. Если я не установил окно в SceneDelegate, я получаю только черный экран на устройствах iOS 13.0+ и, если я не устанавливаю в AppDelegate, я получаю только черный экран на устройствах <13.0 <strong>, так как я вызываю оба файла viewdidload () в Viewcontroller вызывается дважды.

Ниже указано, что мой 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()

}
...
}

Я что-то упустил?

1 Ответ

0 голосов
/ 27 марта 2020

Это можно легко найти с помощью поиска, но вот пример ...

Примечание: ссылка на пользователя SO Matt

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.
    }


}
...