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

SwiftUI中NavigationView内List外侧的Text视图无法显示如何解决

问题原因

你直接将List、Text作为NavigationView的同级子元素,SwiftUI默认会对多个未明确布局容器的子元素使用ZStack堆叠布局,Text会被覆盖在List下方,因此不可见。

解决方法

将NavigationView内的所有需要按垂直顺序排列的元素,统一包裹在VStack容器中即可,如果你还需要添加第二个List,直接放在Text下方即可,修改后参考代码如下:

var body: some View {
    NavigationView {
        // 增加VStack作为根布局容器,所有元素垂直排列
        VStack(spacing: 0) {
            List {
                Button(action: {}, label: {
                    Text("Name: \(name)")
                })
                
                Button(action: {}, label: {
                    Text("Description: \(description)")
                })
                
                Button(action: {}, label: {
                    Text("Numerical Target: \(numTarget)")
                })
                
                Picker(selection: $numUnitIndex, label: Text("Numerical Unit: \(numUnit)")) {
                    ForEach(0 ..< units.count) {
                        Text(self.units[$0]).tag($0).foregroundColor(.blue)
                    }
                }.pickerStyle(MenuPickerStyle())
                
                Button(action: {}, label: {
                    Text("Start date: \(startDate)")
                })
                
                Button(action: {}, label: {
                    Text("End date: \(endDate)")
                })
                
                Button(action: {}, label: {
                    Text("Category: \(category)")
                })
            }.onAppear() {
                setPickerValue()
            }
            .navigationBarTitleDisplayMode(.inline)
            .navigationTitle("Goal Details")
            .toolbar {
                Button("Add Step") {
                    print("Help tapped!")
                }
            }
            
            Text("Steps for this goal")
                .background(Color.black)
                .font(.system(size: 20))
                .foregroundColor(.white) // 建议补充白色文字,避免黑底看不清内容
            
            // 此处添加你的第二个List即可
            List {
                // 填写第二个List的业务内容
            }
        }
    }
}

额外提示

如果你的项目最低兼容iOS 16及以上版本,推荐替换已废弃的NavigationView为NavigationStack,二者基础用法完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 09:27:03