run*_*ter -2 swift swiftui ios15
我试图在Picker所选选项旁边显示单词,但这些单词永远不会正确显示。我希望用户能够单击文本Picker也能够打开。我尝试将文本放在选择器之外,这会导致正确显示,但文本不可单击。我正在使用Xcode 13 Beta 5和iOS15。
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()
}
}
Run Code Online (Sandbox Code Playgroud)
为满足我的要求而进行的调整如下所示。现在,标签内的任何内容都可以单击。
Menu {
Picker(selection: $fruit, label: EmptyView()) {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
}
} label: {
HStack {
Image(systemName: "hand.thumbsup.fill")
Text("Picked: \(fruit)")
}
}
Run Code Online (Sandbox Code Playgroud)
使用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)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1102 次 |
| 最近记录: |