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

NavigationLink跳转视图宽度受限,如何设置为全屏显示?

解决SwiftUI网格中NavigationLink跳转后视图宽度受限问题

问题根源在于你把NavigationView嵌套在了LazyVGrid的单个网格项内部,这会让整个导航栈的宽度被网格项的尺寸限制死,后续所有跳转的子视图都会继承这个窄宽度。

只需要把NavigationView移到HomeView的最外层,作为整个视图的根容器即可解决:

struct HomeView: View {
    
    let columns = [
        GridItem(.adaptive(minimum: 150))
    ]
    
    var body: some View {
        NavigationView { // 将NavigationView移至此处作为根视图
            ScrollView {
                LazyVGrid(columns: columns) {
                    // 移除原网格项内嵌套的NavigationView
                    NavigationLink {
                        WorkoutListView()
                    } label: {
                        VStack {
                            Image(systemName: "figure.run.square.stack.fill")
                                .resizable()
                                .scaledToFit()
                                .frame(width: 100, height: 100)
                                .padding()
                            VStack {
                                Text("Create Workout")
                                    .font(.headline)
                                    .foregroundColor(.white)
                            }
                            .padding(.vertical)
                            .frame(maxWidth: .infinity)
                            .background(Color(red: 0.2, green: 0.2, blue: 0.3))
                        }
                        .clipShape(RoundedRectangle(cornerRadius: 10))
                        .overlay(
                            RoundedRectangle(cornerRadius: 10)
                                .stroke(Color(red: 0.2, green: 0.2, blue: 0.3))
                        )
                    }
                }
                .padding([.horizontal, .bottom])
            }
        }
    }
}

修改后,导航栈的宽度会自适应屏幕尺寸,WorkoutListView和CreateWorkoutView无需任何改动,就能自动占满整个屏幕宽度。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 14:02:42