是否可以迁移旧的 Xcode 项目以使用 SwiftUI?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56515495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:58:05  来源:igfitidea点击:

Is it possible to migrate an old Xcode project to use SwiftUI?

iosswiftxcodeswiftui

提问by Ailton Vieira Pinto Filho

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?

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

I have already tried to add the UIApplicationSceneManifestkey in Info.plist, I changed the AppDelegate.swiftto use scenes, I created the SceneDelegate.swiftand even then I could not

我已经尝试在 中添加UIApplicationSceneManifest密钥Info.plist,我将其更改AppDelegate.swift为使用场景,我创建了SceneDelegate.swift,即使那样我也无法

回答by Matteo Pacini

I assume you're using Xcode 11 GMand macOS Mojaveor Catalina.

我假设您使用的是Xcode 11 GMmacOSMojaveCatalina.

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

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

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>) {

}

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

此外,您需要确保windowSceneDelegate.

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()
}

where ContentViewis the main SwiftUIview you want to display.

您要显示ContentView的主SwiftUI视图在哪里。

P.S. Make sure the plistspecifies $(PRODUCT_MODULE_NAME).SceneDelegateas delegate class name, and the scene delegate is called SceneDelegate

PS确保plist指定$(PRODUCT_MODULE_NAME).SceneDelegate为委托类名,并调用场景委托SceneDelegate

Example:

例子:

enter image description here

在此处输入图片说明

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

如果您打开Catalina,则可以Previews在目标的构建设置中打开。

Build Options -> Enable Previews

构建选项 -> 启用预览



Addendum I:

附录一

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

请确保您删除的故事板从Info.plist中和关键you're targeting iOS 13

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明



Addendum II:

附录二

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

Clean Derived Data,正如评论中的许多开发人员所建议的那样。

回答by zdravko zdravkin

It's a correct only minor change

这是一个正确的只是微小的变化

In SceneDelegate.swiftreplace

SceneDelegate.swift替换

let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIHostingController(rootView: ContentView())
self.window = window
window.makeKeyAndVisible()

with

if let windowScene = scene as? UIWindowScene {
    let window = UIWindow(windowScene: windowScene)
    window.rootViewController = UIHostingController(rootView: ContentView())
    self.window = window
    window.makeKeyAndVisible()
}

TAKEN FROM HERE

取自这里

回答by drekka

My solution turned out to be different. In my case, I had everything in place, but when the code attempted to load the UISceneConfiguration, it failed to load the config in the Info.plistand gave me a secondary window config instead with no scene delegate set. If I asked for the correct configuration from the debug console it would load as expected. I was confused.

结果我的解决方案有所不同。就我而言,我在一切就绪,但是当代码尝试加载UISceneConfiguration,它加载失败中的配置Info.plist,给了我第二个窗口配置,而不是没有现场代表组。如果我从调试控制台请求正确的配置,它将按预期加载。我很困惑。

I double checked everything and tried all of the suggestions here but none worked. In the end I did 'Hardware' - 'Erase all contents and settings...'on the simulator and that solved it.

我仔细检查了所有内容并尝试了这里的所有建议,但都没有奏效。最后,我在模拟器上做了“硬件”-“擦除所有内容和设置...”并解决了它。

My guess is that because I'd been running the pre-SwiftUI version of the app on the simulator, something in that caused the SwiftUI version to behave differently.

我的猜测是,因为我一直在模拟器上运行该应用程序的 SwiftUI 之前版本,所以其中的某些内容导致 SwiftUI 版本的行为有所不同。