ios - 选择器未显示在列表中 | 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
推荐阅读
- c++ - std::map 使用迭代器查找距离,程序不会终止
- java - 我的应用程序运行良好,但现在它不断崩溃
- linkedin - 广告报告 API 的速率限制
- perl - 如何提取可能包含数字的字符串中尖括号括起来的数字
- jquery - jQuery根据'td'内'sub-div'中的内容对表格列进行排序
- spring-boot - 如何在不安装docker和部署在centos的情况下将spring boot应用容器化
- r - 如何根据可变行数创建具有移动平均值的新列?
- loopback4 - 如何在环回 4 中添加包含过滤器
- php - PHP仅在服务器重新启动后挂起
- reactjs - React Hooks - 当我在另一个选择上选择一个选项时更改选择选项