是否可以迁移旧的Xcode项目以使用SwiftUI? [英] Is it possible to migrate an old Xcode project to use SwiftUI?

查看:383
本文介绍了是否可以迁移旧的Xcode项目以使用SwiftUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Main.storyboard用Xcode 10制作的应用程序,并希望将其迁移到使用Apple的新框架:SwiftUI.
已经可以了吗?

I have an app made in Xcode 10 using Main.storyboard and would like to migrate it to use Apple's new framework: SwiftUI.
Is that already possible?

我已经尝试在Info.plist中添加UIApplicationSceneManifest键,我更改了AppDelegate.swift以使用场景,创建了SceneDelegate.swift,即使这样我也无法

I have already tried to add the UIApplicationSceneManifest key in Info.plist, I changed the AppDelegate.swift to use scenes, I created the SceneDelegate.swift and even then I could not

推荐答案

我假设您正在使用Xcode 11 GM和macOS MojaveCatalina.

I assume you're using Xcode 11 GM and macOS Mojave or Catalina.

随着plist中的更改,您必须在应用程序委托中添加UISceneSession生命周期函数.

Along with the changes in the plist, you have to add UISceneSession lifecycle functions in the application delegate.

func application(_ application: UIApplication,
                 configurationForConnecting connectingSceneSession: UISceneSession,
                 options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    // The name must match the one in the Info.plist
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {

}

此外,您需要确保在SceneDelegate中正确创建了window.

Also, you need to make sure the window is created correctly in the SceneDelegate.

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

    guard let windowScene = scene as? UIWindowScene else {
        return
    }

    let window = UIWindow(windowScene: windowScene)
    window.rootViewController = UIHostingController(rootView: ContentView())
    self.window = window
    window.makeKeyAndVisible()
}

其中ContentView是要显示的主SwiftUI视图.

where ContentView is the main SwiftUI view you want to display.

P.S.确保plist$(PRODUCT_MODULE_NAME).SceneDelegate指定为委托类名称,并且场景委托称为SceneDelegate

P.S. Make sure the plist specifies $(PRODUCT_MODULE_NAME).SceneDelegate as delegate class name, and the scene delegate is called SceneDelegate

示例:

如果使用的是Catalina,则可以在目标的构建设置中打开Previews.

If you're on Catalina, you can turn on Previews in the build settings for your target.

构建选项->启用预览

附录I :

确保从Info.Plist和you're targeting iOS 13中删除 Storyboard 键.

Make sure you remove the Storyboard key from the Info.Plist and that you're targeting iOS 13.

附录II :

清理Derived Data,正如评论中的许多开发人员所建议的.

Clean Derived Data, as many devs in the comments suggest.

这篇关于是否可以迁移旧的Xcode项目以使用SwiftUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆