简体   繁体   English

iOS 12 / 13 程序化视图创建

[英]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:从历史上看,我已经删除了我的Main.storyboard ,从我的Info.plist删除了引用并设置我的UIWindowrootViewController如下:

    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 -但是,当尝试在 iOS 13 中运行我的应用程序时,我遇到了崩溃 -

*** 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. iOS 12 仍按预期运行。 How should I setup my view programmatically to support iOS 12 and 13?我应该如何以编程方式设置我的视图以支持 iOS 12 和 13?

You need to add update SceneDelegate.swift also.您还需要添加更新SceneDelegate.swift

Update func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) and add更新func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)并添加

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

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

        self.window = window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 视图控制器响应iOS 12中的应用程序委托通知,但不响应iOS 13中的应用委托通知 - View controller responds to app delegate notifications in iOS 12 but not in iOS 13 在iOS12中以编程方式访问电池信息 - Programmatic access to battery information in iOS12 在支持 iOS 12 和 iOS 13 时无法设置根视图 controller - Unable to set root view controller while supporting iOS 12 & iOS 13 touchesBegan 在 iOS 12 中被调用,但在 iOS 13 中不被调用 - touchesBegan is called in iOS 12 but is not called in iOS 13 UITableViewRowAction 的图标显示在 iOS 13 上,但不在 iOS 12 上 - Icon for UITableViewRowAction is shown on iOS 13, but not on iOS 12 Xcode 13 iOS 15 编程 Obj-C 约束 translatesAutoresizingMaskIntoConstraints - Xcode 13 iOS 15 Programmatic Obj-C Constraints translatesAutoresizingMaskIntoConstraints iOS 12 和 13 中的 UISearchbar UI 不同且不正确 - UISearchbar UI in iOS 12 and 13 are different and not correct 支持 iOS 12 和 13 时的 AppDelegate 和 SceneDelegate - AppDelegate and SceneDelegate when supporting iOS 12 and 13 在iOS中动态创建视图 - Dynamic view creation in iOS Xcode iOS 13+ 和 12 的 13 个通用链接 - Xcode 13 Universal Links for both iOS 13+ and 12
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM