英文:
SwiftUI Picker in List adding extra row on top when using sections
问题
I have a SwiftUI inline picker in a list. It works fine; however, when I enclose it in a section, an empty row appears on top of each section with an inline picker. Is there any way to remove this empty row? If I remove the section, the empty row disappears.
英文:
I have a SwiftUI inline picker in a list. It works fine however when I enclose it in a section, an empty row appears on top of each section with an inline picker. Is there anyway to remove this empty row? If I remove the section the empty row disappears.
@State var color: String = "Red"
var colors: [String] = ["Red", "Green", "Blue"]
var body: some View {
List {
Section(header: Text("Colors")) {
Picker("", selection: $color) {
ForEach(colors, id: \.self) { color in
Text(color)
}
}
.pickerStyle(InlinePickerStyle())
}
}
}
var body: some View {
List {
Picker("", selection: $color) {
ForEach(colors, id: \.self) { color in
Text(color)
}
}
.pickerStyle(InlinePickerStyle())
}
}
答案1
得分: 1
将init(selection:content:label:)
中的init
更改为空标签即可解决问题。
Section("颜色") {
Picker(selection: $color) {
ForEach(colors, id: \.self) { color in
Text(color)
}
} label: {}
.pickerStyle(.inline)
}
或者如@workingdogsupportUkraine在评论中提到的,您也可以使用修饰符`.labelsHidden()`
Section(header: Text("颜色")) {
Picker("Picker", selection: $color) {
ForEach(colors, id: \.self) { color in
Text(color)
}
}
.pickerStyle(InlinePickerStyle())
.labelsHidden()
}
英文:
Changing the init to init(selection:content:label:)
with an empty label fixes the issue
Section("Colors") {
Picker(selection: $color) {
ForEach(colors, id: \.self) { color in
Text(color)
}
} label: {}
.pickerStyle(.inline)
}
or as mentioned in the comments by @workingdogsupportUkraine you can also use the modifier .labelsHidden()
Section(header: Text("Colors")) {
Picker("Picker", selection: $color) {
ForEach(colors, id: \.self) { color in
Text(color)
}
}
.pickerStyle(InlinePickerStyle())
.labelsHidden()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论