简体   繁体   English

SwiftUI Segmented Picker 元素为灰色且已禁用

[英]SwiftUI Segmented Picker element is grey and disabled

I've added a Picker (SegmentedPickerStyle) to my project using SwiftUI as shown:我已经使用 SwiftUI 在我的项目中添加了一个 Picker (SegmentedPickerStyle),如下所示:

Picker(selection: $selectedType, label: Text("Type")) {
      Text("Translation")
      Text("Highlights")
}
.pickerStyle(SegmentedPickerStyle())

But for some reason, the result (both in canvas and the simulator) is a grey, disabled picker (please see screenshot attached).但出于某种原因,结果(在画布和模拟器中)是一个灰色的、禁用的选择器(请参见附上的屏幕截图)。

图片

What I try to achieve:我试图实现的目标: 图 2 Does anyone has an idea what am I doing wrong?有谁知道我做错了什么?

Try as following尝试如下

@State private var selectedType = 0

...

Picker(selection: $selectedType, label: Text("Type")) {
      Text("Translation").tag(0)
      Text("Highlights").tag(1)
}
.pickerStyle(SegmentedPickerStyle())

Your picker has 2 Text items, "Translation" & "Highlights".您的选择器有 2 个文本项目,“翻译”和“亮点”。 So your selection will be either of those Strings.因此,您的选择将是这些字符串中的任何一个。 Create a @State variable to track when that selection changes and to show the new 'State'.创建一个@State 变量以跟踪该选择何时更改并显示新的“状态”。

@State private var selectedType = "Translation"
...
Picker(selection: $selectedType, label: Text("Type")) {
      Text("Translation")
      Text("Highlights")
}
.pickerStyle(SegmentedPickerStyle())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM