简体   繁体   中英

iOS 12 / 13 Programmatic View Creation

My apps uses no Storyboards. All views are created programmatically.

Historically I have deleted my Main.storyboard , removed the reference from my Info.plist and setup my UIWindow and rootViewController as follows:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = UIViewController()
        window.makeKeyAndVisible()

        self.window = window

        return true
    }

However when trying to run my app in iOS 13, I get a crash -

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle NSBundle </Users/Dev/Library/Developer/CoreSimulator/Devices/8A4474B1-FCA3-4720-8F34-A6975A03EE19/data/Containers/Bundle/Application/258FA246-A283-4FE6-A075-58BD32424427/Home.app> (loaded)'
***

iOS 12 still runs as expected. How should I setup my view programmatically to support iOS 12 and 13?

You need to add update SceneDelegate.swift also.

Update func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) and add

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

        let viewController = ViewController()
        window.rootViewController = viewController
        window.makeKeyAndVisible()

        self.window = window

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM