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

SwiftUI NavigationView嵌套容器点击仅局部跳转 如何实现全屏跳转

问题根因

你将NavigationView放在了每个IndividualButtonView内部,每个按钮对应一个仅覆盖自身区域的独立导航栈,因此跳转仅会在按钮所在的局部区域生效,不会占满全屏。

修复步骤

1. 修改ListOfButtonsView.swift

将NavigationView移到最外层,作为覆盖整个屏幕的全局导航容器:

struct ListOfButtonsView: View {
    
    let buttonNames = ["Button 1", "Button 2", "Button 3"]
    
    var body: some View {
        NavigationView {
            VStack {
                ForEach(0..<buttonNames.count) { button in
                    IndividualButtonView()
                }
            }
            .navigationBarHidden(true)
        }
    }
}

2. 修改IndividualButtonView.swift

删除内部的NavigationView,复用外层的全局导航容器:

struct IndividualButtonView: View {
        
    var body: some View {
        NavigationLink(destination: CirclesView()) {
            VStack {
                Text("Button")
                    .font(.largeTitle)
                    .padding(32)
            }
            .frame(maxWidth: .infinity)
            .foregroundColor(.white)
            .background(Color.red)
            .cornerRadius(12)
            .padding()
        }
    }
}

3. CirclesView.swift无需调整

你现有CirclesView的代码逻辑符合需求,不需要修改。


可选优化:如果你的项目最低适配iOS 16+,可以将NavigationView替换为苹果当前更推荐的NavigationStack,核心逻辑和上述修改完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 14:18:03