选择器标签更改为选定值 SwiftUI

d0t*_*b0t 2 picker ios swift swiftui

我想要一个恒定的标签,但我的选择器标签会根据我选择的值而变化

我想要一个恒定的标签,但我的选择器标签会根据我选择的值而变化。

我的代码:

Picker(
                        selection : $textBoxes[currentIndex].textFont,
                        label : Text("Font"),
                        content : {
                            Text("LuxuriousRoman-Regular").tag("LuxuriousRoman-Regular")
                            Text("Merriweather-Regular").tag("Merriweather-Regular")
                            Text("Neonderthaw-Regular").tag("Neonderthaw-Regular")
                            Text("OpenSansCondensed-Light").tag("OpenSansCondensed-Light")
                            Text("Pacifico").tag("Pacifico")
                            Text("PTSans-Regular").tag("PTSans-Regular")
                            Text("RobotoMono-VariableFont_wght").tag("RobotoMono-VariableFont_wght")
                            Text("SedgwickAve-Regular").tag("SedgwickAve-Regular")
                        }
                    ).pickerStyle(MenuPickerStyle())
Run Code Online (Sandbox Code Playgroud)

Sco*_*man 6

Picker以其菜单样式形式使用时,label不会显示 ,而是使用所选项目,如您所见。

如果你想使用标准标签,你应该使用Menu作为基础视图,然后使用 aPicker作为菜单的内容。例如,您可以在此处看到用法的差异:

struct ContentView: View {
  enum DemoOption: String, CaseIterable {
    case one, two, three, four, five
  }
  
  @State private var pickerValue: DemoOption = .one
  @State private var menuValue: DemoOption = .two
  
  var body: some View {
    VStack {
      Picker("Picker Option", selection: $pickerValue) {
        ForEach(DemoOption.allCases, id: \.rawValue) { option in
          Text(option.rawValue).tag(option)
        }
      }
      .pickerStyle(.menu)
      
      Menu {
        Picker("Choose an option", selection: $menuValue) {
          ForEach(DemoOption.allCases, id: \.rawValue) { option in
            Text(option.rawValue).tag(option)
          }
        }
      } label: {
        Text("Choose an option")
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述