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

RealityKit项目中SwiftUI页面与AR视图的Segue跳转问题

解决RealityKit项目中SwiftUI页面跳转问题

要实现先显示SegueView、点击按钮后跳转至AR视图ContentView,核心是修改App入口的根视图,并在SegueView中添加跳转逻辑,具体步骤如下:

1. 修改App入口的根视图

找到你的App结构体(通常命名为xxxApp),将默认的根视图从ContentView替换为SegueView,这样App启动时会先加载欢迎页面:

@main
struct ARDemoApp: App {
    var body: some Scene {
        WindowGroup {
            SegueView() // 替换原有的ContentView()
        }
    }
}

2. 在SegueView中实现跳转逻辑

根据iOS版本选择合适的跳转方式:

方式一:使用NavigationStack(iOS 16+ 推荐)

通过NavigationStack包裹页面,并使用NavigationLink实现页面跳转:

struct SegueView: View {
    var body: some View {
        NavigationStack {
            VStack(spacing: 40) {
                Text("AR Demo")
                    .font(.largeTitle)
                    .bold()
                
                NavigationLink(destination: ContentView()) {
                    Text("See in AR")
                        .font(.title2)
                        .padding(.horizontal, 30)
                        .padding(.vertical, 12)
                        .background(Color.blue)
                        .foregroundColor(.white)
                        .cornerRadius(12)
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color(UIColor.systemBackground))
        }
    }
}

方式二:使用状态变量切换视图(兼容iOS 13+)

通过@State变量控制当前显示的视图,点击按钮后切换至AR页面:

struct SegueView: View {
    @State private var showARView = false
    
    var body: some View {
        ZStack {
            if showARView {
                ContentView()
                    .transition(.opacity) // 添加过渡动画
            } else {
                VStack(spacing: 40) {
                    Text("AR Demo")
                        .font(.largeTitle)
                        .bold()
                    
                    Button(action: {
                        withAnimation {
                            showARView = true
                        }
                    }) {
                        Text("See in AR")
                            .font(.title2)
                            .padding(.horizontal, 30)
                            .padding(.vertical, 12)
                            .background(Color.blue)
                            .foregroundColor(.white)
                            .cornerRadius(12)
                    }
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(Color(UIColor.systemBackground))
            }
        }
    }
}

3. 确保ContentView是正确的AR视图

你的ContentView需要正确封装ARView,示例代码如下:

struct ContentView: View {
    var body: some View {
        ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}

struct ARViewContainer: UIViewRepresentable {
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        // 这里添加你的AR渲染逻辑(比如加载模型、配置AR会话等)
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {}
}

问题根源说明

之前启动直接显示ContentView,是因为App入口的根视图被设置为ContentView,修改为SegueView后就能先展示欢迎页面,通过点击按钮触发跳转逻辑进入AR视图。

内容的提问来源于stack exchange,提问作者Arcee

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 14:24:19