基本上我使用選單選擇器樣式來顯示一組數字。我更喜歡它而不是選單,因為它在所選專案前面沒有復選標記,并且因為我需要更改選單的文本,如果新專案比前一個專案長,則需要時間重新渲染它。當使用選擇器樣式時,我得到了這些讓我煩惱的雙箭頭,到目前為止還沒有找到解決方案。
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)")
}
}
}
我希望它看起來像那樣。(這張照片取自普通選單,但正如我之前所說,它有更多底片,所以我不需要它)
uj5u.com熱心網友回復:
您可以嘗試如下使用 Menu 可以顯示復選標記
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])")
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/522256.html
上一篇:無法在Xcode中處理API資料并快速解碼為json
下一篇:如何處理MongodbNull值