提问人:Boyan Pavlov 提问时间:10/27/2022 更新时间:10/27/2022 访问量:930
SwiftUI 菜单选取器样式显示奇怪的双箭头
SwiftUI Menu Picker Style shows strange double arrow
问:
基本上,我正在使用菜单选择器样式来显示一组数字。我更喜欢它而不是菜单,因为它在所选项目前面没有复选标记,并且因为我需要更改菜单的文本,如果新项目比前一个项目长,则需要时间重新呈现它。 使用选择器样式时,我得到了这些双箭头,这让我很恼火,到目前为止还没有找到解决方案。
struct ContentView: View {
@State private var selection = "Red"
let colors = ["Red", "Green", "Blue", "Black", "Tartan"]
var body: some View {
VStack {
Picker("Select a paint color", selection: $selection) {
ForEach(colors, id: \.self) {
Text($0)
}
}
.pickerStyle(.menu)
Text("Selected color: \(selection)")
}
}
}
我希望它看起来像那样。(这张照片是从普通菜单中拍摄的,但正如我之前所说,它有更多的底片,所以我不需要它)
答:
0赞
AdR
10/27/2022
#1
您可以尝试如下菜单,可以显示复选标记
struct ContentView: View {
let colors = ["Red", "Green", "Blue", "Black", "Tartan"]
@State var index = 0
@State var selectedColor = "Red"
var body: some View {
VStack {
Menu{
ForEach(Array(colors.enumerated()), id: \.offset){index, color in
Button(action: {
self.index = index
self.selectedColor = color
}, label: {
HStack{
Text(color)
if selectedColor == colors[index]{
Image(systemName: "checkmark")
}
}
})
}
} label: {
Text(colors[index])
.foregroundColor(.blue)
.frame(width: UIScreen.main.bounds.width/2)
}
Text("Selected Color \(colors[index])")
}
}
}
评论