簡體   English   中英

列表中未顯示選擇器 | SwiftUI

[英]Picker not showing inside List | 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: "")
    }
}

你可以試試這個,或者它的一些變體:

        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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM