-2

我正在尝试在Picker所选选项旁边显示单词,但这些单词从未正确显示。我希望用户能够单击文本也能够打开Picker。我尝试将文本放在选择器之外,这会导致正确显示,但文本不可点击。我正在使用Xcode 13 Beta 5iOS15

struct ContentView: View {
    @State var fruit = "Apple"
    let fruits = ["Apple","Orange","Pear","Grape"]
    
    var body: some View {
        HStack {
            Picker(selection: $fruit) {
                Text("Picked:")
                ForEach(fruits, id: \.self){ fruit in
                    Text(fruit)
                }
            } label: {
                Text("")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Picker带有文字的图片Picker 选择器

根据我的要求进行的调整如下所示。任何放在标签内的东西现在都可以点击了。

Menu {
    Picker(selection: $fruit, label: EmptyView()) {
        ForEach(fruits, id: \.self) { fruit in
            Text(fruit)
        }
    }
} label: {
      HStack {
          Image(systemName: "hand.thumbsup.fill")
          Text("Picked: \(fruit)")
      }
}
4

1 回答 1

2

使用Menu带有内联的 aPicker作为其内容:

Menu {
    Picker(selection: $fruit, label: EmptyView()) {
        ForEach(fruits, id: \.self) { fruit in
            Text(fruit)
        }
    }
    .labelsHidden()
    .pickerStyle(InlinePickerStyle())
} label: {
    HStack {
        Text("Picked:")
        Text(fruit)
    }
}
于 2021-08-23T19:00:07.090 回答