You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否将基于Xcode 10的旧项目迁移至SwiftUI框架?

当然可行!不过先给你提个关键前提:Xcode 10本身并不支持SwiftUI——SwiftUI是苹果在Xcode 11和iOS 13版本才正式推出的框架,所以你第一步得把Xcode升级到11或更高版本,不然根本没法用上SwiftUI。

假设你已经完成了Xcode升级,那咱们来梳理你可能遗漏的细节,帮你把迁移走通:

完整迁移步骤梳理

1. 确认Info.plist的场景配置结构正确

你已经添加了UIApplicationSceneManifest键,但要确保它的子项配置完整:

  • 把UIApplicationSupportsMultipleScenes设为YES
  • 在UISceneConfigurations下的UIWindowSceneSessionRoleApplication数组中,新增一个配置项:
    • 键UISceneDelegateClassName的值填$(PRODUCT_MODULE_NAME).SceneDelegate(确保模块名和项目一致)
    • 如果不需要保留原Storyboard的入口,可以不用填UISceneStoryboardFile,后续直接用SwiftUI作为根视图

2. 调整AppDelegate.swift的代码

原来基于Storyboard的AppDelegate里的window属性和相关初始化代码要删掉,改成支持场景会话的逻辑:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // 返回场景配置,这里的名称要和Info.plist里的配置对应
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // 可选:处理场景销毁后的清理逻辑
    }
}

3. 正确实现SceneDelegate.swift

这一步是把SwiftUI视图接入的关键,你需要在场景连接时创建SwiftUI的根视图:

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // 创建你的SwiftUI主视图
        let contentView = ContentView()

        // 绑定window到当前场景
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            // 用UIHostingController包裹SwiftUI视图作为根控制器
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

    // 以下是可选的场景生命周期方法,按需实现
    func sceneDidDisconnect(_ scene: UIScene) {}
    func sceneDidBecomeActive(_ scene: UIScene) {}
    func sceneWillResignActive(_ scene: UIScene) {}
    func sceneWillEnterForeground(_ scene: UIScene) {}
    func sceneDidEnterBackground(_ scene: UIScene) {}
}

4. 创建SwiftUI视图文件

新建一个ContentView.swift,作为你的SwiftUI入口视图:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, SwiftUI!")
            .font(.title)
            .padding()
    }
}

// 预览视图,方便在Xcode中实时查看效果
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

5. 检查项目部署目标

在项目设置里,把Deployment Target改成iOS 13.0或更高版本——SwiftUI只支持iOS 13及以上系统。

渐进式迁移建议

如果不想一次性替换掉所有Storyboard内容,你也可以在现有的ViewController中嵌入SwiftUI视图,实现渐进式迁移:

// 在你的现有ViewController中添加这段代码
let swiftUIHost = UIHostingController(rootView: ContentView())
addChild(swiftUIHost)
view.addSubview(swiftUIHost.view)
// 调整SwiftUI视图的位置和大小
swiftUIHost.view.frame = CGRect(x: 20, y: 100, width: view.bounds.width - 40, height: 200)
swiftUIHost.didMove(toParent: self)

这样你就能逐步把部分界面替换成SwiftUI,不用一次性重构整个项目。

内容的提问来源于stack exchange,提问作者Ailton Vieira Pinto Filho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:09:45