首页 > 解决方案 > 选择器未显示在列表中 | SwiftUI

问题描述

我的应用程序上有一个详细信息屏幕,由于某种原因,当我尝试将选择器放在列表中时,我得到一个空的灰色框,只有在这个灰色框内滚动才能看到它。有人能帮助我吗?

注意:我是 iOS 开发的初学者 :-)

谢谢

来自模拟器的照片

我的代码:

struct GoalDetailsView: View {
    
    var id: String
    var authorId: String
    var name: String
    var description: String
    var startDate: String
    var endDate: String
    var numTarget: String
    var numUnit: String
    var category: String
    
    @State var numUnitIndex = 0
    
    var units = ["other", "Kg", "$$", "Km", "Hours", "Days", "Weeks", "%"]

    var body: some View {
        
        NavigationView {
            
            List {
                Button(action: {}, label: {
                    Text("Name: \(name)")
                })
                
                Button(action: {}, label: {
                    Text("Description: \(description)")
                })
                
                Button(action: {}, label: {
                    Text("Numerical Target: \(numTarget)")
                })
                
                Form {
                    Section {
                        Picker(selection: $numUnitIndex, label: Text("Numerical Unit")) {
                            ForEach(0 ..< units.count) {
                                Text(self.units[$0]).tag($0).foregroundColor(.blue)
                            }
                        }
                    }
                }

                
                    
                Spacer()
            }.navigationBarTitleDisplayMode(.inline)
            .navigationTitle("Goal Details")
        }
        
    }
}

struct GoalDetailsView_Previews: PreviewProvider {
    static var previews: some View {
        GoalDetailsView(id: "", authorId: "", name: "", description: "", startDate: "", endDate: "", numTarget: "", numUnit: "", category: "")
    }
}

标签: iosswiftswiftui

解决方案


你可以试试这个,或者它的一些变体:

        Form {
            Section {
                Picker(selection: $numUnitIndex, label: Text("Numerical Unit")) {
                    ForEach(0 ..< units.count) {
                        Text(self.units[$0]).tag($0).foregroundColor(.blue)
                    }
                }
                .pickerStyle(.inline)  // <-- here
            }
        }.scaledToFit()  // <-- here

推荐阅读